Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get one item from an array of name,value JSON

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') 
like image 866
Omu Avatar asked Aug 16 '11 08:08

Omu


People also ask

How do you get a specific value from an array in JavaScript?

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() .

How do you parse an array of JSON objects?

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.

Can JSON parse array?

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.

What is a JsonArray?

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.


2 Answers

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.

like image 182
Langdon Avatar answered Oct 08 '22 23:10

Langdon


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 
like image 32
nnnnnn Avatar answered Oct 09 '22 00:10

nnnnnn