I have the following JSON array :
我有以下JSON数组:
{
"list": [{
"name": "Assignment",
"id": 67,
"template": 0,
"children": [{
"assignmentnumber": 1000,
"fromCountry": "Sweden",
"toCountry": "Spain",
"fromCity": "Stockholm",
"toCity": "Madrid"
}, {
"assignmentnumber": 15678,
"fromCountry": "Sweden",
"toCountry": "Germany",
"fromCity": "Stockholm",
"toCity": "Berlin"
}, {
"assignmentnumber": 10001,
"fromCountry": "Sweden",
"toCountry": "United Kingdom",
"fromCity": "Stockholm",
"toCity": "London"
}]
}, {
"name": "Valuation form",
"id": 36,
"template": 0,
"children": [15678]
}, {
"name": "Claim",
"id": 12,
"template": 0,
"children": [1000, 10001]
}]
}
I need to extract a new array from the array containing only the 'name' element. I have tried lodash but cannot figure out how to use it properly.
我需要从只包含“name”元素的数组中提取一个新的数组。我试过lodash,但不知道如何正确地使用它。
Anyone who can give me a clue how to do this ?
谁能告诉我怎么做这个?
3 个解决方案
#1
4
You should use map
method, which accepts as parameter a callback
function.
您应该使用map方法,它接受回调函数作为参数。
The map() method creates a new array with the results of calling a provided function on every element in this array.
map()方法创建一个新的数组,其结果是在这个数组中的每个元素上调用提供的函数。
var array=obj.list.map(callback);
function callback(item){
return item.name;
}
Or simply:
或者仅仅是:
var array=obj.list.map(el=>el.name);
var obj={
"list": [{
"name": "Assignment",
"id": 67,
"template": 0,
"children": [{
"assignmentnumber": 1000,
"fromCountry": "Sweden",
"toCountry": "Spain",
"fromCity": "Stockholm",
"toCity": "Madrid"
}, {
"assignmentnumber": 15678,
"fromCountry": "Sweden",
"toCountry": "Germany",
"fromCity": "Stockholm",
"toCity": "Berlin"
}, {
"assignmentnumber": 10001,
"fromCountry": "Sweden",
"toCountry": "United Kingdom",
"fromCity": "Stockholm",
"toCity": "London"
}]
}, {
"name": "Valuation form",
"id": 36,
"template": 0,
"children": [15678]
}, {
"name": "Claim",
"id": 12,
"template": 0,
"children": [1000, 10001]
}]
};
console.log(obj.list.map(function(item){
return item.name;
}));
#2
0
You can do this without lodash, using native Array APIs.
使用本机数组api,无需使用lodash,也可以这样做。
const obj = { /* The JSON in your question */ };
const names = obj.list.map(item => item.name);
Array.map()
takes a function and calls that function on each item of an array, returning a new array containing the result of each item.
map()获取一个函数,并在数组的每个项上调用该函数,返回包含每个项结果的新数组。
#3
0
Map is the best way to go as everyone else stated. Another alternative is use a forEach loop, specially if you want to operate with them.
地图是大家都说的最好的方式。另一种选择是使用forEach循环,特别是如果您想使用它们。
var children = obj.children;
var array_of_names;
children.forEach(function (item)){
array_of_names.push(item.name);
//... Do more operations for each item
}
#1
4
You should use map
method, which accepts as parameter a callback
function.
您应该使用map方法,它接受回调函数作为参数。
The map() method creates a new array with the results of calling a provided function on every element in this array.
map()方法创建一个新的数组,其结果是在这个数组中的每个元素上调用提供的函数。
var array=obj.list.map(callback);
function callback(item){
return item.name;
}
Or simply:
或者仅仅是:
var array=obj.list.map(el=>el.name);
var obj={
"list": [{
"name": "Assignment",
"id": 67,
"template": 0,
"children": [{
"assignmentnumber": 1000,
"fromCountry": "Sweden",
"toCountry": "Spain",
"fromCity": "Stockholm",
"toCity": "Madrid"
}, {
"assignmentnumber": 15678,
"fromCountry": "Sweden",
"toCountry": "Germany",
"fromCity": "Stockholm",
"toCity": "Berlin"
}, {
"assignmentnumber": 10001,
"fromCountry": "Sweden",
"toCountry": "United Kingdom",
"fromCity": "Stockholm",
"toCity": "London"
}]
}, {
"name": "Valuation form",
"id": 36,
"template": 0,
"children": [15678]
}, {
"name": "Claim",
"id": 12,
"template": 0,
"children": [1000, 10001]
}]
};
console.log(obj.list.map(function(item){
return item.name;
}));
#2
0
You can do this without lodash, using native Array APIs.
使用本机数组api,无需使用lodash,也可以这样做。
const obj = { /* The JSON in your question */ };
const names = obj.list.map(item => item.name);
Array.map()
takes a function and calls that function on each item of an array, returning a new array containing the result of each item.
map()获取一个函数,并在数组的每个项上调用该函数,返回包含每个项结果的新数组。
#3
0
Map is the best way to go as everyone else stated. Another alternative is use a forEach loop, specially if you want to operate with them.
地图是大家都说的最好的方式。另一种选择是使用forEach循环,特别是如果您想使用它们。
var children = obj.children;
var array_of_names;
children.forEach(function (item)){
array_of_names.push(item.name);
//... Do more operations for each item
}