2

I have an array of objects below

const response = 
[
{
    id: 105,
    label: 'test',
    directories: [ '/api/1/directories/500' ]
  },
  {
    id: 337,
    label: 'test2',
    directories: [ '/api/1/directories/766' ]
  },
  {
    id: 200,
    label: 'test20',
    directories: [ '/api/1/directories/95' ]
  }
]

And I have an array called directories

directories = [ '/api/1/directories/766', '/api/1/directories/95' ]

I am trying to search/filter response to only return the ids of objects that matches the directories in directories array

so the sample output can be something like below, which is only the IDs of the matched objects

sampleOut= [337, 200]
AbdA
  • 790
  • 7
  • 16
  • Does this answer your question? [Javascript find json value](https://stackoverflow.com/questions/19253753/javascript-find-json-value) – Mihail Minkov Jun 07 '20 at 04:31

5 Answers5

3

This could be done as follows:

const out = response.filter(o => directories.includes(o.directories[0])).map(o => o.id);

Please have a look at the following runnable code snippet.

const response = [
  {
    id: 105,
    label: 'test',
    directories: [ '/api/1/directories/500' ]
  },
  {
    id: 337,
    label: 'test2',
    directories: [ '/api/1/directories/766' ]
  },
  {
    id: 200,
    label: 'test20',
    directories: [ '/api/1/directories/95' ]
  }
];

const directories = [ '/api/1/directories/766', '/api/1/directories/95' ];

const out = response.filter(o => directories.includes(o.directories[0])).map(o => o.id);
console.log(out)
uminder
  • 23,831
  • 5
  • 37
  • 72
2
const sampleOut = response.filter(response => directories.includes(response.directories[0]))
    .map(response => response.id);
chiragrtr
  • 902
  • 4
  • 6
2

You can use reduce and check exist by indexOf as

const response = 
[
{
    id: 105,
    label: 'test',
    directories: [ '/api/1/directories/500' ]
  },
  {
    id: 337,
    label: 'test2',
    directories: [ '/api/1/directories/766' ]
  },
  {
    id: 200,
    label: 'test20',
    directories: [ '/api/1/directories/95' ]
  }
]

directories = [ '/api/1/directories/766', '/api/1/directories/95' ];

var result = response.reduce((acc, item)=>{

  if(directories.indexOf(item.directories[0]) > -1){
    acc.push(item.id);
    
  }
  return acc;
},[]);

console.log(result);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • I am getting UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined at Array.reduce () – AbdA Jun 07 '20 at 04:44
  • never mind it worked the array was declared empty first after I filled it in I got the right data – AbdA Jun 07 '20 at 04:51
2

You could do something like this.

const response = 
[
{
    id: 105,
    label: 'test',
    directories: [ '/api/1/directories/500' ]
  },
  {
    id: 337,
    label: 'test2',
    directories: [ '/api/1/directories/766' ]
  },
  {
    id: 200,
    label: 'test20',
    directories: [ '/api/1/directories/95' ]
  }
];

const directories = [ '/api/1/directories/766', '/api/1/directories/95' ];

const res = response.reduce((acc, elem)=>{
  if(directories.includes(elem.directories[0])){
    acc.push(elem.id)
  }
  return acc
},[]);

console.log(res)
ABGR
  • 4,631
  • 4
  • 27
  • 49
1

You can iterate through the main array and pushing the propertie you want if there is a match:

const response = 
[
{
    id: 105,
    label: 'test',
    directories: [ '/api/1/directories/500' ]
  },
  {
    id: 337,
    label: 'test2',
    directories: [ '/api/1/directories/766' ]
  },
  {
    id: 200,
    label: 'test20',
    directories: [ '/api/1/directories/95' ]
  }
]

var directories = [ '/api/1/directories/766', '/api/1/directories/95' ]

var sampleOut = []

response.forEach(function(a) {
  if (directories.indexOf(a.directories[0]) >= 0 ) {
    sampleOut.push(a.id)
  }
})

console.log(sampleOut)
djcaesar9114
  • 1,880
  • 1
  • 21
  • 41