0

JS how do you get the sibling of the chosen item from array.

var list =[ [title, description], [title, description] [title, description]. ] etc. var input = ‘blue’; I got the title(s) and checked if each matche input. If the title matches input, return title along with description. (return all matching items) I used filter to check if title matches input. My approach to match the description with the chosen title is to get the index of “title”, and get sibling item (description). https://code.sololearn.com/WqHO2f4qX9T9/?ref=app

16th Jan 2021, 4:28 AM
Ginfio
Ginfio - avatar
4 ответов
+ 2
Ipang ok, let me make it clear: var input = ‘abcd’ var list = [ [‘oook’, ‘desc’], [‘abcd’, ‘bla’], [‘abcd’, ‘ohohoh’] ] i used .filter() to get all the items that match input. In this case above, the second and the third items match the input. Now i want to get their description (of the items that matched input). Expected output something like this: abcd bla abcd ohohoh
16th Jan 2021, 7:57 PM
Ginfio
Ginfio - avatar
+ 1
I don't understand this paragraph "My approach to match the description with the chosen title is to get the index of “title”, and get sibling item (description)." Are you checking <match> against "title" only, or also against "description"? What do you need the index for? Do you want the index in the filtered result?
16th Jan 2021, 7:22 AM
Ipang
+ 1
var filtered = arr.reduce(function(res,val,idx,arr) { return val[0]==match ? res.concat(idx) : res; },[])
16th Jan 2021, 1:34 PM
visph
visph - avatar
+ 1
var filtered = arr.reduce(function(res,val,idx,arr) { return val[0]==match ? res.concat(idx) : res; },[])
16th Jan 2021, 1:34 PM
visph
visph - avatar