简体   繁体   中英

How to get all the values associated with an object in array?

var array = [
    { name:"test0", value:"0" },
    { name:"test1", value:"1" },
    { name:"test2", value:"2" },
    { name:"test0", value:"3" },
    { name:"test0", value:"4" }
];

How do I get the value(s) of all the element names associated with test0 using a loop in javascript?

SAMPLE OUTPUT

{0,3,4}

Just filter the values and map the desired ones.

array.filter allows you to filter items of an array with a condition, so using i => i.name === 'test0' will return a new array where only the values with name 'test0' are acquired. Finally, using map you create a new array, whose values are the result of the callback function provided. So, using map(i => i.value) will return the property .value of each (filtered) object.

If you're expecting the values to be numeric, remember to cast them to integers.

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; var res = array.filter(i => i.name === 'test0').map(i => i.value); console.log(res);

Otherwise, you can do that in a single shot with reduce:

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; var res = array.reduce((a,b) => { return b.name === 'test0' && a.push(+b.value), a; }, []); console.log(res);

In this last example, the values are casted to integers using the unary operator ( + ).

Finally, as requested, here is another approach not relying on any array prototype:

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; // init a new empty array. var res = []; for (var i = 0; i < array.length; i++) { var item = array[i]; // not mandatory but easy enough to read: acquire the current looped value. if (item.name === 'test0') { // if the looped item name is 'test0', join the if. res.push(Number(item.value)); // if so, push the item's value, and cast it to a number (using Number). } } console.log(res); // log the result.

You can using array filter. Docs here

const result = array.filter(a=> a.name === "test0");

console.log(result.map(w => w.value));

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; var test0 = array.filter(item => item.name == "test0").map(item => item.value); console.log(test0);

You could filter the objects and get a numerival value with mapping.

 var array = [{ name: "test0", value: "0" }, { name: "test1", value: "1" }, { name: "test2", value: "2" }, { name: "test0", value: "3" }, { name: "test0", value: "4" }], result = array .filter(({ name }) => name === 'test0') .map(({ value }) => +value); console.log(result);

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; var Rs=[]; for(var i in array){ if(array[i]["name"]=="test0") Rs.push(i);//Rs.push(array[i]["value"]); } console.log(Rs);

Simple loop is beast!

 var array = [ { name:"test0", value:"0" }, { name:"test1", value:"1" }, { name:"test2", value:"2" }, { name:"test0", value:"3" }, { name:"test0", value:"4" } ]; let output = array.reduce((acc, {name, value}) => { if(name === 'test0') { return [...acc, value]; } return acc; }, []); console.log(output)

create a generic function in which you just need to pass the key value and array and it will filter out the arry on the basic of parameters  

  var array = [
        { name:"test0", value:"0" },
        { name:"test1", value:"1" },
        { name:"test2", value:"2" },
        { name:"test0", value:"3" },
        { name:"test0", value:"4" }
    ];

    function filterList(keyValue, list) {
        let filteredList = [];
        for(let i = 0; i < list.length; i++) {
            if(list[i]["name"] === keyValue) {
                filteredList.push(list[i]["value"]);
            }
        }
        return filteredList;
    }

    console.log(filterList("test0", array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM