I have this array:
var arr = []; arr.push({name:"k1", value:"abc"}); arr.push({name:"k2", value:"hi"}); arr.push({name:"k3", value:"oa"});
is it possible to do get the value or a specific element by knowing the name ?
something like this:
arr['k2'].value
or
arr.get('k1')
JavaScript Demo: Array.find() If you need to find the index of a value, use Array.prototype.indexOf() . (It's similar to findIndex() , but checks each element for equality with the value instead of using a testing function.) If you need to find if a value exists in an array, use Array.prototype.includes() .
Parsing JSON Data in JavaScript In JavaScript, you can easily parse JSON data received from the web server using the JSON. parse() method. This method parses a JSON string and constructs the JavaScript value or object described by the string. If the given string is not valid JSON, you will get a syntax error.
Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.
I know this question is old, but no one has mentioned a native solution yet. If you're not trying to support archaic browsers (which you shouldn't be at this point), you can use array.filter
:
var arr = []; arr.push({name:"k1", value:"abc"}); arr.push({name:"k2", value:"hi"}); arr.push({name:"k3", value:"oa"}); var found = arr.filter(function(item) { return item.name === 'k1'; }); console.log('found', found[0]);
Check the console.
You can see a list of supported browsers here.
In the future with ES6, you'll be able to use array.find.
Arrays are normally accessed via numeric indexes, so in your example arr[0] == {name:"k1", value:"abc"}
. If you know that the name
property of each object will be unique you can store them in an object instead of an array, as follows:
var obj = {}; obj["k1"] = "abc"; obj["k2"] = "hi"; obj["k3"] = "oa"; alert(obj["k2"]); // displays "hi"
If you actually want an array of objects like in your post you can loop through the array and return when you find an element with an object having the property you want:
function findElement(arr, propName, propValue) { for (var i=0; i < arr.length; i++) if (arr[i][propName] == propValue) return arr[i]; // will return undefined if not found; you could return a default instead } // Using the array from the question var x = findElement(arr, "name", "k2"); // x is {"name":"k2", "value":"hi"} alert(x["value"]); // displays "hi" var y = findElement(arr, "name", "k9"); // y is undefined alert(y["value"]); // error because y is undefined alert(findElement(arr, "name", "k2")["value"]); // displays "hi"; alert(findElement(arr, "name", "zzz")["value"]); // gives an error because the function returned undefined which won't have a "value" property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With