Dev
Dev

Reputation: 423

Find specific key value in array of objects using nodejs

We have a JSON object like

var jsons = {
    "LAPTOP": [{
        "productId": "123"
    }],
    "DESKTOP": [{
        "productId": "456"
    }],
    "MOUSE": [{
        "productId": "789"
    }, {
        "productId": "012"
    }],
    "KEY-BOARD": [{
        "productId": "345"
    }]
}

if we search { "productId": "012" } it should return key DESKTOP It would be great if we can use lodash

Upvotes: 0

Views: 2894

Answers (7)

vincent
vincent

Reputation: 2181

Consider using object-scan. It's powerful once you wrap your head around it.

// const objectScan = require('object-scan');

const search = (term, input) => {
  const r = objectScan(['**.productId'], {
    abort: true,
    rtn: 'key',
    filterFn: ({ value }) => value === term
  })(input);
  return r === undefined ? r : r[0];
};

const jsons = { LAPTOP: [{ productId: '123' }], DESKTOP: [{ productId: '456' }], MOUSE: [{ productId: '789' }, { productId: '012' }], 'KEY-BOARD': [{ productId: '345' }] };

console.log(search('012', jsons));
// => MOUSE
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22534

You can use keys and find.

var jsons = { "LAPTOP": [{ "productId": "123" }], "DESKTOP": [{ "productId": "456" }], "MOUSE": [{ "productId": "789" }, { "productId": "012" }], "KEY-BOARD": [{ "productId": "345" }] },
    value = { "productId": "012" };

var result = _.find(_.keys(jsons), key => _.find(jsons[key], value));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Upvotes: 0

Gruff Bunny
Gruff Bunny

Reputation: 27976

A solution using lodash:

let result = _(jsons)
    .pickBy(item => _.some(item, {productId: '123'}))
    .keys()
    .head();

First we find the key where the value contains and object with the matching productId. We then take the first key from the collection of keys.

Upvotes: 2

fingeron
fingeron

Reputation: 1182

You can use Javascript's key in object for loop syntax like so:

var jsons = {
"LAPTOP": [{
    "productId": "123"
}],
"DESKTOP": [{
    "productId": "456"
}],
"MOUSE": [{
    "productId": "789"
}],
"KEY-BOARD": [{
    "productId": "345"
}]};

var searchFor = "789";

for(var key in jsons) 
  if(jsons.hasOwnProperty(key))
    if(jsons[key][0] && jsons[key][0].productId === searchFor)
      alert(key); // MOUSE

Upvotes: 0

Lalit
Lalit

Reputation: 1369

var jsons = {
"LAPTOP": [{
    "productId": "123"
}],
"DESKTOP": [{
    "productId": "456"
}],
"MOUSE": [{
    "productId": "789"
}, {
    "productId": "012"
}],
"KEY-BOARD": [{
    "productId": "345"
}]
};

var id = "789";
for(item in jsons){
  jsons[item].forEach(function(data){
    if(data.productId == id){
      console.log(item);
    }
  });
}

Upvotes: 0

prabhatojha
prabhatojha

Reputation: 2085

Try this

var jsons = {
"LAPTOP": [{
    "productId": "123"
}],
"DESKTOP": [{
    "productId": "456"
}],
"MOUSE": [{
    "productId": "789"
}, {
    "productId": "012"
}],
"KEY-BOARD": [{
    "productId": "345"
}]};

var result = Object.keys(jsons).find((key) => {
	   return jsons[key].find((item) => item.productId === '123')
    });

console.log(result);
    

Upvotes: 2

Aswin Ramesh
Aswin Ramesh

Reputation: 1674

without LODASH you can have something like this

var toSearch = 789;
Object.keys(jsons).forEach((key) => {
  if(jsons[key].find((pid) => pid.productId == toSearch)){
    console.log(key)
  }
})

Upvotes: 0

Related Questions