[英]Iterate over Nested Objects in Array and delete the Property names and Values
我有如下数组,我想要做的是根据另一个数组中的值从下面的数组中删除属性名称,即键和值。
选项是创建一个新数组,如下所示,仅包含来自 Keep Array 的值,或者从原始数组中删除未在“keep”数组中列出的值。
var Keep=["ItemID","ItemNumber","OptionNo","Quantity","Price","Description","StockStatus","Url"];
var originalarray=[
{
"ItemID": 1,
"ItemNumber": "611741",
"OptionNo": "22",
"SizeDescription": "3-6 Mths",
"Price": "14.00",
"Quantity": 1,
"StockStatus": "instock",
"StockMessage": "In Stock",
"WarrantyItem": null,
"Description": "Coral/Blue Embellished Two In One Dress (3mths-7yrs)",
"Url": "/g82272s2",
"FulfilmentType": ""
},
{
"ItemID": 2,
"ItemNumber": "912767",
"OptionNo": "13",
"SizeDescription": "11 EU 29",
"Price": "16.00",
"Quantity": 1,
"StockStatus": "instock",
"StockMessage": "In Stock",
"WarrantyItem": null,
"Description": "Silver Buckle Corkbed Sandals (Younger)",
"Url": "/g82272s2",
"CustomItemFields": [],
"FulfilmentType": ""
}
]
我试图创建新数组,但该数组不是嵌套的,因此我在同一个键中有多个条目,具有不同的值,如下所示:
["ItemID:1",
"ItemNumber:611741",
"OptionNo:22",
"Price:14.00",
"Quantity:1",
"StockStatus:instock",
"Description:Coral/Blue Embellished Two In One Dress (3mths-7yrs)", "Url:/g82272s2",
"ItemCategory:Dresses",
"ItemID:2",
"ItemNumber:912767",
"OptionNo:13",
"Price:16.00",
"Quantity:1",
"StockStatus:instock",
"Description:Silver Buckle Corkbed Sandals (Younger)",
"Url:/g82272s2",
"ItemCategory:Sandals"]
I want the result array to be like this
[{"ItemID:1",
"ItemNumber:611741",
"OptionNo:22",
"Price:14.00",
"Quantity:1",
"StockStatus:instock",
"Description:Coral/Blue Embellished Two In One Dress (3mths-7yrs)", "Url:/g82272s2",
"ItemCategory:Dresses"},
{"ItemID:2",
"ItemNumber:912767",
"OptionNo:13",
"Price:16.00",
"Quantity:1",
"StockStatus:instock",
"Description:Silver Buckle Corkbed Sandals (Younger)",
"Url:/g82272s2",
"ItemCategory:Sandals"}]
这是我的代码:
var keep=["ItemID", "ItemNumber", "OptionNo", "Quantity", "Price", "Description", "ItemCategory", "StockStatus", "Url"],z={}; z=new Array(); y.forEach(function(arrays){
// 4 arrays in a
var x=Object.keys(arrays);
var k=Object.values(arrays);
//console.log(x);
//console.log(y);
for(var i = 0; i < x.length; i++) {
//console.log(x[i]);
keep.forEach(function(item,index,array){
if(item==x[i]){
//z.push(x[i]+":"+y[i]);
z.push(x[i]+":"+k[i]);
return z;
}
})
}
})
希望你能帮忙。
谢谢你。
更新:
为此,这里是lodash版本,也可能更容易阅读:
const keep = (k, l) => _.map(l, o => _.pick(o, k)) const keepThese = ["ItemID", "ItemNumber", "OptionNo", "Quantity", "Price", "Description", "StockStatus", "Url"] const orig = [{"ItemID": 1, "ItemNumber": "611741", "OptionNo": "22", "SizeDescription": "3-6 Mths", "Price": "14.00", "Quantity": 1, "StockStatus": "instock", "StockMessage": "In Stock", "WarrantyItem": null, "Description": "Coral/Blue Embellished Two In One Dress (3mths-7yrs)", "Url": "/g82272s2", "FulfilmentType": ""}, {"ItemID": 2, "ItemNumber": "912767", "OptionNo": "13", "SizeDescription": "11 EU 29", "Price": "16.00", "Quantity": 1, "StockStatus": "instock", "StockMessage": "In Stock", "WarrantyItem": null, "Description": "Silver Buckle Corkbed Sandals (Younger)", "Url": "/g82272s2", "CustomItemFields": [], "FulfilmentType": ""}] console.log(keep(keepThese, orig))
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
下面的keep
函数将执行此操作,尽管例如使用lodash可能有更优雅的方法。
const keep = (k, l) => l.map(o => Object.entries(o).reduce((r, [n, v]) => k.includes(n) ? (r[n] = v, r) : r, {})) const keepThese = ["ItemID", "ItemNumber", "OptionNo", "Quantity", "Price", "Description", "StockStatus", "Url"] const orig = [{"ItemID": 1, "ItemNumber": "611741", "OptionNo": "22", "SizeDescription": "3-6 Mths", "Price": "14.00", "Quantity": 1, "StockStatus": "instock", "StockMessage": "In Stock", "WarrantyItem": null, "Description": "Coral/Blue Embellished Two In One Dress (3mths-7yrs)", "Url": "/g82272s2", "FulfilmentType": ""}, {"ItemID": 2, "ItemNumber": "912767", "OptionNo": "13", "SizeDescription": "11 EU 29", "Price": "16.00", "Quantity": 1, "StockStatus": "instock", "StockMessage": "In Stock", "WarrantyItem": null, "Description": "Silver Buckle Corkbed Sandals (Younger)", "Url": "/g82272s2", "CustomItemFields": [], "FulfilmentType": ""}] console.log(keep(keepThese, orig))
说明: .map()
输入到Object.entries()
.reduce()
到一个Object
,从{}
开始。
所以(r, [n, v]) => k.includes(n) ? (r[n] = v, r) : r
(r, [n, v]) => k.includes(n) ? (r[n] = v, r) : r
为数组中每个对象的每个属性运行。 如果属性数组要保留.includes()
当前属性的名称,则将相应的属性 & 值添加到r
。 否则不要触摸r
。 对下一个属性重复,最后将r
添加到结果数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.