我有一个这样的 json 数组,我只想将 productId 提取到一个数组中。
{
"products": [
{
"productId": "a01",
"uuid": "124748ba-6fc4f"
},
{
"productId": "b2",
"uuid": "1249b9ba-64d"
},
{
"productId": "c03",
"uuid": "124c78da-64"
},
{
"productId": "d04",
"uuid": "124ee9da-6"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Javascript 中做到这一点。我不太擅长 JS,请帮助我。谢谢
Ray*_*yon 12
该
map()方法创建一个新数组,其结果是对该数组中的每个元素调用提供的函数。(arr.map(callback[, thisArg]))
var input = {
"products": [{
"productId": "a01",
"uuid": "124748ba-6fc4f"
}, {
"productId": "b2",
"uuid": "1249b9ba-64d"
}, {
"productId": "c03",
"uuid": "124c78da-64"
}, {
"productId": "d04",
"uuid": "124ee9da-6"
}]
};
var op = input.products.map(function(item) {
return item.productId;
});
//Using arrow function-
//var op = input.products.map(item => item.productId);
console.log(op);Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5972 次 |
| 最近记录: |