1

I have json data below

var dataA = {
   "state":"TX",
   "city":"Dallas",
   "code":"75201",
   "totalareas":"75201",
   "website":"asdf.ccom"      


}

and I have an array of objects

var dataB =[    
    {
     "key":"state",
     "value":"TX"

    },
    {
    "key":"city",
    "value":"Dallas"
    },
    {
     "key":"yu",
     "value":"2"
    },
   {
     "key":"website",
     "value":"asdf.ccom"
    }
    ]

I want to compare this dataA and dataB and get only matched fields with object how can I do this

My expectation result

{
   "city":"Dallas",
   "state":"TX",

}
realme
  • 33
  • 2
  • 8
  • What is your Question? the Output is inside the first JSON right? – chintuyadavsara Oct 12 '18 at 09:11
  • There is no JSON in your question at all. Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Oct 12 '18 at 09:12
  • I want to compare this dataA and dataB and get only matched fields – realme Oct 12 '18 at 09:12

1 Answers1

4

You can use reduce to loop thru dataB. Convert each object into an array using Object.entries and use forEach to loop thru. If the key exist and the same value, assign the key and value to the accumulator.

var dataA = {
  "state": "TX",
  "city": "Dallas",
  "code": "75201",
  "totalareas": "75201"
}

var dataB = [{
  "city": "Dallas",
  "citycode": "",
  "population": "234k"
}, {
  "state": "TX",
  "statecode": "asdf5678"
}, {
  "zip": "75201",
  "areacount": "567"
}]


var result = dataB.reduce((c, v) => {
  Object.entries(v).forEach(([i, o]) => {
    if (dataA[i] && dataA[i] === o) c[i] = o;
  })
  return c;
}, {});


console.log(result);

UPDATE: You dont need to convert each object into an array. You can use just use the key and value on the condition.

var dataA = {"state":"TX","city":"Dallas","code":"75201","totalareas":"75201","website":"asdf.ccom"}
var dataB = [{"key":"state","value":"TX"},{"key":"city","value":"Dallas"},{"key":"yu","value":"2"},{"key":"website","value":"asdf.ccom"}]

var result = dataB.reduce((c, v) => {
  if (dataA[v.key] && dataA[v.key] === v.value) c[v.key] = v.value;
  return c;
}, {});

console.log(result);
Eddie
  • 26,593
  • 6
  • 36
  • 58
  • hai @Eddie can you pls help me out on this i have changed dataB object now i want to comapre – realme Oct 16 '18 at 08:05
  • @realme you change it to? – Eddie Oct 16 '18 at 08:07
  • hey sorry dataB look like this var dataA = {"state":"TX","city":"Dallas","code":"75201","totalareas":"75201","website":"asdf.ccom"} var dataB = [{"key":"state","value":"TX"},{"key":"city","value":"Dallas"},{"key":"yu","value":"2"},{"key":"website","value":"asdf.ccom"}] – realme Oct 16 '18 at 08:41
  • see now thay changed like this var dataB = [{"key":"state","value":"TX"},{"key":"city","value":"Dallas"},{"key":"yu","value":"2"},{"key":"website","value":"asdf.ccom"}] but ur updated code is not working – realme Oct 16 '18 at 08:44
  • Happy to help :) – Eddie Oct 16 '18 at 08:59