Last active
January 8, 2023 10:02
-
-
Save YagoLopez/1c2fe87d255fc64d5f1bf6a920b67484 to your computer and use it in GitHub Desktop.
Deep search javascript object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Attribution: http://techslides.com/how-to-parse-and-search-json-in-javascript */ | |
//return an array of objects according to key, value, or key and value matching | |
function getObjects(obj, key, val) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getObjects(obj[i], key, val)); | |
} else | |
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not) | |
if (i == key && obj[i] == val || i == key && val == '') { // | |
objects.push(obj); | |
} else if (obj[i] == val && key == ''){ | |
//only add if the object is not already in the array | |
if (objects.lastIndexOf(obj) == -1){ | |
objects.push(obj); | |
} | |
} | |
} | |
return objects; | |
} | |
//return an array of values that match on a certain key | |
function getValues(obj, key) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getValues(obj[i], key)); | |
} else if (i == key) { | |
objects.push(obj[i]); | |
} | |
} | |
return objects; | |
} | |
//return an array of keys that match on a certain value | |
function getKeys(obj, val) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { | |
objects = objects.concat(getKeys(obj[i], val)); | |
} else if (obj[i] == val) { | |
objects.push(i); | |
} | |
} | |
return objects; | |
} | |
var json = '{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","ID":"44","str":"SGML","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}'; | |
var js = JSON.parse(json); | |
//example of grabbing objects that match some key and value in JSON | |
console.log(getObjects(js,'ID','SGML')); | |
//returns 1 object where a key names ID has the value SGML | |
//example of grabbing objects that match some key in JSON | |
console.log(getObjects(js,'ID','')); | |
//returns 2 objects since keys with name ID are found in 2 objects | |
//example of grabbing obejcts that match some value in JSON | |
console.log(getObjects(js,'','SGML')); | |
//returns 2 object since 2 obects have keys with the value SGML | |
//example of grabbing objects that match some key in JSON | |
console.log(getObjects(js,'ID','')); | |
//returns 2 objects since keys with name ID are found in 2 objects | |
//example of grabbing values from any key passed in JSON | |
console.log(getValues(js,'ID')); | |
//returns array ["SGML", "44"] | |
//example of grabbing keys by searching via values in JSON | |
console.log(getKeys(js,'SGML')); | |
//returns array ["ID", "SortAs", "Acronym", "str"] |
Or are you trying to search on the key? In that case you could just specify the needle as *
? Is that what you're looking for? If you could provide more details with expected output I'd be able to help better
@mzcoder-hub I'd really recommend you take a look at the readme here . It details all the basic use cases and it sound like yours is one of them. Your question is not very clear and so I can't answer it. So you have to options:
- go to the readme and figure out what you need to know yourself
- reformulate the question in a way that I can understand (ideally with example results!) and I will do my best to provide a working solution
Thank you, L~
Is this what you want?
const objectScan = require('object-scan');
const data = { merah: [{ harga: 30000, sku: 'SKU-123asd-asdasd', stok: '20', ukuran: 'XL', warna: 'Merah', _id: '6069895a0e277a332ca19e49' }, { harga: 30000, sku: 'SKU-123asd-asdasda', stok: '20', ukuran: 'L', warna: 'Merah', _id: '606b6ea7ff4a142508df2a98' }], hijau: [{ harga: 30000, sku: 'SKU-123asd-asdasds', stok: '20', ukuran: 'XL', warna: 'Hijau', _id: '606b6ea7ff4a142508df2a98' }, { harga: 30000, sku: 'SKU-123asd-asdasdsa', stok: '20', ukuran: 'L', warna: 'Hijau', _id: '606b6ea7ff4a142508df2a98' }] };
console.log(objectScan(['*[*]'])(data));
// => [ [ 'hijau', 1 ], [ 'hijau', 0 ], [ 'merah', 1 ], [ 'merah', 0 ] ]
Still guessing what you want... how about this?
const objectScan = require('object-scan');
const data = { merah: [{ harga: 30000, sku: 'SKU-123asd-asdasd', stok: '20', ukuran: 'XL', warna: 'Merah', _id: '6069895a0e277a332ca19e49' }, { harga: 30000, sku: 'SKU-123asd-asdasda', stok: '20', ukuran: 'L', warna: 'Merah', _id: '606b6ea7ff4a142508df2a98' }], hijau: [{ harga: 30000, sku: 'SKU-123asd-asdasds', stok: '20', ukuran: 'XL', warna: 'Hijau', _id: '606b6ea7ff4a142508df2a98' }, { harga: 30000, sku: 'SKU-123asd-asdasdsa', stok: '20', ukuran: 'L', warna: 'Hijau', _id: '606b6ea7ff4a142508df2a98' }] };
const r = objectScan(['*[*]'], { rtn: 'value' })(data);
console.log(JSON.stringify(r));
// => [{"harga":30000,"sku":"SKU-123asd-asdasdsa","stok":"20","ukuran":"L","warna":"Hijau","_id":"606b6ea7ff4a142508df2a98"},{"harga":30000,"sku":"SKU-123asd-asdasds","stok":"20","ukuran":"XL","warna":"Hijau","_id":"606b6ea7ff4a142508df2a98"},{"harga":30000,"sku":"SKU-123asd-asdasda","stok":"20","ukuran":"L","warna":"Merah","_id":"606b6ea7ff4a142508df2a98"},{"harga":30000,"sku":"SKU-123asd-asdasd","stok":"20","ukuran":"XL","warna":"Merah","_id":"6069895a0e277a332ca19e49"}]
@YagoLopez Thanks! Exactly what I need and it works great
@YagoLopez Thanks! Exactly what I need and it works great
You´re welcome @androcado. Please consider give a start to the gist if you found it useful. Thanks 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure, just use
**
as a search needle and filter withfilterFn
usingvalue
. It's all in the docs with detailed examples. Let me know if you're struggling