last day (14 days later) » 

1:38 PM
1
A: javascript - search array of nested objects and return parent if value is found in child

Nina ScholzYou could use an iterative and recursive approach and return the result of the check and take then the element with Array#filter function checkObject(obj, val) { function iter(o) { return Object.keys(o).some(function (k) { if (typeof o[k] === 'object') { ...

 
that's great. it works fine. thanks a lot. how can I return parent with only child/children that has search string ? I mean not returning all child even if search string has been found in only one child.
 
do you mean the parent object?
 
yes parent with only child that has search string found.
for example if search string has found within 2 child out of 20 children, the function should return parent with 2 child only. currently it's returning parent with all 20 children.
I've updated data in fiddle jsfiddle.net/7eL7t0d1/7. If I search for string 'emc', function should return 2 objects. first object should have only one child where manufacturer is emc.
did you able to make the function works in that way ? I am still not able to figure it out from the function you have created.
 
actually i don't know, what you really want. if the inner object has a sub string wiht the wanted part, the outer element is returned.
 
yes but if there are more than one inner object and sub string has found with only one inner (child) object then I need outer (parent) with only one inner (child) object. Not with all inner (child) object.
 
1:38 PM
please define inner and outer object actually your structure is array0 object0 array1 object1 property.
 
I've given example in update 2 of the question description.
Hi
 
your solution works fine but I want outer object with only child that has string matched
 
di you tried your data with my proposal?
ok, i think i understand. then you have to mutate the object inside
 
yes
I tried to create a new object and store filtered objects into it
 
1:47 PM
but if you get the result, the original object is mutate.
 
but I couldn't able to make it work
 
or you create a whole new object
 
yes actually this is for table search. and search result will be given as new data to table to bind
search always will be made on original data
 
so you need a copy?
 
yes copy or a new data set (filtered)
I couldn't figure out in your function where do I get filtered data to store it to the new object
 
1:51 PM
ok. wait a minute
 
yeah sure
 
2:09 PM
 
2:47 PM
Hi
did you able to make it work ?
 
it is so easy as it might look
 
I tried but couldn't make it work
 
3:12 PM
I am only getting matched inner object in case of string is found. I am not able to get parent object of matched child's object.
 
4:05 PM
Hi Icepickle, can you show me in this fiddle jsfiddle.net/7eL7t0d1/7
 
you may have a look to the question,
if it is what you want.
 
I've just posted a new question with reference to my previous one
 
it's not good, because its still the old question
 
Oh. I will make it sure from next time.
 
maybe you check the old question and if the answer helps you, you could close the new question.
or you close the old question and i answer at the new question.
 
4:13 PM
whatever you suggest
shall I close the old one ? and I will try with the new answer you will post it to my new question
I've closed the old question.
@NinaScholz I will click on "That solved my problem" button once I get new acceptable answer on my new question. Is that fine ?
@NinaScholz I tried your new solution. But when I search for a string 'invalid', I am not getting nested object 'asset_info' in a result.
 
Would it make sense to add the chatlog to the new question btw? Could be interesting
 
4:30 PM
@Icepickle I don't understand how could it be interesting ?
 
well, there is quite some info in here, that isn't part of the question + what failed before. though you are correct, I did find the chat through the previous question
 
@Icepickle if you insist I can add this to the new question. how do I do that ?
 
It's okay, it's like you said, it was findable. I updated my answer btw to do the copy without babel, and it also works with the invalid search ;)
but if you really want to you could copy the link in
 
chatlog link added to the new question
I will be trying both solutions tomorrow and will let you immediately
Thanks @NinaScholz and @Icepickle
 
4:54 PM
it should work now.
 

last day (14 days later) »