简体   繁体   English

如何获取与数组中的对象关联的所有值?

[英]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?如何使用 javascript 中的循环获取与test0关联的所有元素名称的值?

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. array.filter允许您使用条件过滤数组的项目,因此使用i => i.name === 'test0'将返回一个新数组,其中仅获取名称为 'test0' 的值。 Finally, using map you create a new array, whose values are the result of the callback function provided.最后,使用map创建一个新数组,其值是提供的回调函数的结果。 So, using map(i => i.value) will return the property .value of each (filtered) object.因此,使用map(i => i.value)将返回每个(过滤后的)对象的属性.value

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:否则,您可以使用 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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