Search code examples
javascriptarraysjsonjavascript-objects

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


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.


Solution

  • 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.