0

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

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I vote this cause its optimized and wont do unnecessary loops.
0

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

Comments

0

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

Comments

0

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

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.