This question already has an answer here:
这个问题在这里已有答案:
- From an array of objects, extract value of a property as array 12 answers
从一组对象中,提取属性的值作为数组12的答案
I have this json. I only care about the uniqueIDs.
我有这个json。我只关心uniqueIDs。
How can I get ONLY the uniqueID values delivered back to me as a comma separated list, ie, 11111, 22222? (I need to create my own array.) I can't edit the json below - I am just trying to parse the value I care about out of it....
我怎样才能将唯一ID值作为逗号分隔列表传递给我,即11111,22222? (我需要创建自己的数组。)我无法编辑下面的json - 我只是想解析我关心的值....
{
products: [{
type: "Unique",
name: "Joe",
description: "Joes Description",
uniqueID: "11111"
}, {
type: "Unique",
name: "Jane",
description: "Janes Description",
uniqueID: "22222"
}]
}
Thought it would be this easy but its not...
以为这会很容易,但不是......
$data['uniqueID'][0]
3 个解决方案
#1
0
Use map function:
使用地图功能:
var foo = {
metadata: {
products: [{
type: "Unique",
name: "Joe",
description: "Joes Description",
uniqueID: "11111"
} {
type: "Unique",
name: "Jane",
description: "Janes Description",
uniqueID: "22222"
}]
}
}
var ids = foo.metadata.products.map(x => x.uniqueID);
And if you are not familiar with arrow function:
如果你不熟悉箭头功能:
var ids = foo.metadata.products.map(function(x){
return x.uniqueID;
});
#2
0
The Underscore Way
You can use underscore js _.pluck()
or _.map()
function.
您可以使用下划线js _.pluck()或_.map()函数。
var data = {
metadata: {
products: [{
type: "Unique",
name: "Joe",
description: "Joes Description",
uniqueID: "11111"
},
{
type: "Unique",
name: "Jane",
description: "Janes Description",
uniqueID: "22222"
}]
}
};
//Use plunk method
var uniqueIDArr = _.pluck(data.metadata.products, 'uniqueID');
console.log(uniqueIDArr);
//=> ["11111", "22222"]
//Use map function
var uniqueIDArr = _.map(data.metadata.products, function(obj) {
return obj.uniqueID;
});
console.log(uniqueIDArr);
//=> ["11111", "22222"]
<script src="https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js"></script>
#3
0
You can use Array.prototype.map() to create a new array and chain a Array.prototype.join() to get the uniqueIDsString
in a single line:
您可以使用Array.prototype.map()创建一个新数组并链接一个Array.prototype.join()以在一行中获取uniqueIDsString:
var obj = {metadata: {products: [{type: "Unique",name: "Joe",description: "Joes Description",uniqueID: "11111"}, {type: "Unique",name: "Jane",description: "Janes Description",uniqueID: "22222"}]}},
uniqueIDsString = obj
.metadata
.products
.map(function(product) {
return product.uniqueID;
})
.join(', ');
console.log(uniqueIDsString);
#1
0
Use map function:
使用地图功能:
var foo = {
metadata: {
products: [{
type: "Unique",
name: "Joe",
description: "Joes Description",
uniqueID: "11111"
} {
type: "Unique",
name: "Jane",
description: "Janes Description",
uniqueID: "22222"
}]
}
}
var ids = foo.metadata.products.map(x => x.uniqueID);
And if you are not familiar with arrow function:
如果你不熟悉箭头功能:
var ids = foo.metadata.products.map(function(x){
return x.uniqueID;
});
#2
0
The Underscore Way
You can use underscore js _.pluck()
or _.map()
function.
您可以使用下划线js _.pluck()或_.map()函数。
var data = {
metadata: {
products: [{
type: "Unique",
name: "Joe",
description: "Joes Description",
uniqueID: "11111"
},
{
type: "Unique",
name: "Jane",
description: "Janes Description",
uniqueID: "22222"
}]
}
};
//Use plunk method
var uniqueIDArr = _.pluck(data.metadata.products, 'uniqueID');
console.log(uniqueIDArr);
//=> ["11111", "22222"]
//Use map function
var uniqueIDArr = _.map(data.metadata.products, function(obj) {
return obj.uniqueID;
});
console.log(uniqueIDArr);
//=> ["11111", "22222"]
<script src="https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js"></script>
#3
0
You can use Array.prototype.map() to create a new array and chain a Array.prototype.join() to get the uniqueIDsString
in a single line:
您可以使用Array.prototype.map()创建一个新数组并链接一个Array.prototype.join()以在一行中获取uniqueIDsString:
var obj = {metadata: {products: [{type: "Unique",name: "Joe",description: "Joes Description",uniqueID: "11111"}, {type: "Unique",name: "Jane",description: "Janes Description",uniqueID: "22222"}]}},
uniqueIDsString = obj
.metadata
.products
.map(function(product) {
return product.uniqueID;
})
.join(', ');
console.log(uniqueIDsString);