Iterate over Nested Objects in Array and delete the Property names and Values

我与影子孤独终老i 提交于 2019-12-14 03:34:50

问题


I have the array as below and what I want to do is to remove the Property names i.e keys and values from the below array based on the values in the other array.

Options would be to create a new array as below with only values from the Keep Array or to remove the values which are not listed in the "keep" array from the original array.

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": ""
      }
    ]

I tried to get the new array created but the array is not nested so I have multiple entries in for the same key with different values as below:

["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"}]

This is my Code:

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;
                    }
  })
 }
  })

Hope you can help.

Thank you.


回答1:


Update:
For the sake of it, here's the lodash version, may be easier to read, too:

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>

Docs: _.map(), _.pick()


The following keep function will do this, although there might be a more elegant way using lodash, for example.

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))

Explanation: .map() the input to Object.entries() .reduce()'d to an Object, starting from {}.
So (r, [n, v]) => k.includes(n) ? (r[n] = v, r) : r runs for each property in each object in the array. If the array of properties to keep .includes() the current property's name, add the corresponding property & value to r. Otherwise don't touch r. Repeat for next property, at the end add r to result array.




回答2:


I recommend you study the use of map() and filter() and even find().

See the documentation for using Map and Filter or Find.

With them you can perform various validations and transformations on objects in your array.

And to exclude a key with the value of an object you will need:

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];


来源:https://stackoverflow.com/questions/55912901/iterate-over-nested-objects-in-array-and-delete-the-property-names-and-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!