我有下面的数组,我要做的是根据另一个数组中的值从下面的数组中删除属性名称,即键和值。
选项如下所示,仅使用Keep数组中的值创建一个新数组,或从原始数组中删除“ 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;
}
})
}
})
希望您能提供帮助。
谢谢。
答案 0 :(得分:0)
更新 :
因此,这里的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>
例如,尽管使用lodash可能会有更优雅的方式,但是下面的keep
函数将做到这一点。
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()
从Object
开始.reduce()
到{}
。
因此(r, [n, v]) => k.includes(n) ? (r[n] = v, r) : r
为数组中每个对象中的每个属性运行。
如果属性数组保留.includes()
为当前属性的名称,请将相应的属性和值添加到r
。否则,请勿触摸r
。重复下一个属性,最后在结果数组中添加r
。
答案 1 :(得分:-1)