var array = [
{ name:"test0", value:"0" },
{ name:"test1", value:"1" },
{ name:"test2", value:"2" },
{ name:"test0", value:"3" },
{ name:"test0", value:"4" }
];
如何使用JavaScript中的循环获取与test0
关联的所有元素名称的值?
样品输出
{0,3,4}
答案 0 :(得分:0)
只需过滤值并映射所需的值即可。
array.filter
允许您过滤带有条件的数组的项,因此使用i => i.name === 'test0'
将返回一个新数组,其中仅获取名称为'test0'的值。最后,使用map
创建一个新数组,其值是所提供的回调函数的结果。因此,使用map(i => i.value)
将返回每个(已过滤)对象的属性.value
。
如果期望值是数字,请记住将它们转换为整数。
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);
否则,您可以使用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);
在最后一个示例中,使用一元运算符(+
)将值转换为整数。
最后,根据要求,这是另一种不依赖任何数组原型的方法:
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.
答案 1 :(得分:0)
您可以使用数组过滤器。 Docs here
const result = array.filter(a=> a.name === "test0");
console.log(result.map(w => w.value));
答案 2 :(得分:0)
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);
答案 3 :(得分:0)
您可以过滤对象并通过映射获得数值。
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);
答案 4 :(得分:0)
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);
简单的循环就是野兽!
答案 5 :(得分:0)
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)
答案 6 :(得分:0)
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));