nam vo
nam vo

Reputation: 3437

How to filter out nested objects in JSON array?

This is my JSON object array which contains nested objects :

var obj=   [
       {
          "ASIN":"BCXXXXX1",
          "VariationAttributes":[
             {
                "Name":"hand_orientation",
                "Value":"Left Hand"
             },
             {
                "Name":"shaft_material_type",
                "Value":"KBS Max 90 Steel"
             }
          ]
       },
       {
          "ASIN":"BCXXXXX2",
          "VariationAttributes":[
             {
                "Name":"hand_orientation",
                "Value":"Right Hand"
             },
             {
                "Name":"shaft_material_type",
                "Value":"KBS Max 90 Steel"
             }
          ]
       }
    ]

var curState=[
   {
      "Name":"hand_orientation",
      "Value":"Left Hand"
   },
   {
      "Name":"shaft_material_type",
      "Value":"KBS Max 90 Steel"
   }
]

Now I want to filter out items that have variationAattributes that match curState.

var result = obj.map(m=>m.VariationAttributes).filter(search, curState);


function search(item){
  return Object.keys(this).some((key) => item[key] === this[key]);
}

The result is blank. You can play with the code at https://playcode.io./518049

Upvotes: 0

Views: 65

Answers (3)

Rijosh
Rijosh

Reputation: 1544

var obj = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" }] },{"ASIN":"BCXXXXX2",	"VariationAttributes": [{			"Name": "hand_orientation","Value": "Right Hand"}, {"Name": "shaft_material_type",	"Value": "KBS Max 90 Steel"}]}];
var curState = [{"Name": "hand_orientation", "Value": "Left Hand"},{"Name":"shaft_material_type", "Value": "KBS Max 90 Steel"}];

var finalResult = obj.filter(parentObj => {
    return JSON.stringify(parentObj.VariationAttributes) === JSON.stringify(curState);
});
console.log(finalResult)

The variable finalResult will have the filtered array.

Upvotes: 1

Nick Parsons
Nick Parsons

Reputation: 50759

You can use .filter() to filter your arr based on the callback function. The callback function will return true if .some() name or value attribute from the current objects VariationAttributes property doesn't equal that of its corresponding object in the curState object like so:

const arr = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] }, { "ASIN": "BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Right Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] } ];
const curState = [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ];

const res = arr.filter(
  obj => obj.VariationAttributes.some(({Name, Value}, i) => curState[i] && (Name !== curState[i].Name || Value !== curState[i].Value))
);

console.log(res);

You could also use JSON.stringifiy() on your curState so you can compare string versions of VariationAttributes against the string version of the VariationAttributes property:

const arr = [{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] }, { "ASIN": "BCXXXXX2", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Right Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ] } ];
const curState = [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" } ];

const strCurState = JSON.stringify(curState);
const res = arr.filter(
  obj => strCurState !== JSON.stringify(obj.VariationAttributes)
);

console.log(res);

Upvotes: 1

Alex - Tin Le
Alex - Tin Le

Reputation: 2000

Have a look at this.

var obj=[{ "ASIN": "BCXXXXX1", "VariationAttributes": [{ "Name": "hand_orientation", "Value": "Left Hand" }, { "Name": "shaft_material_type", "Value": "KBS Max 90 Steel" }] },{"ASIN":"BCXXXXX2",	"VariationAttributes": [{			"Name": "hand_orientation","Value": "Right Hand"}, {"Name": "shaft_material_type",	"Value": "KBS Max 90 Steel"}]}];
var curState=[{"Name": "hand_orientation", "Value": "Left Hand"},{"Name":"shaft_material_type", "Value": "KBS Max 90 Steel"}];

//var result = obj.map(m=>m.VariationAttributes).filter(search, curState);

var result = obj.filter(item => {
  return item.VariationAttributes.every(attr => {
    return curState.find(state => attr.Name === state.Name && attr.Value === state.Value);
  })
});

console.log(result);

function search(item){
  return Object.keys(this).some((key) => item[key] === this[key]);
}
//console.log(obj)

Upvotes: 1

Related Questions