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]
asked Jun 7, 2020 at 4:27
1

5 Answers 5

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)

answered Jun 7, 2020 at 4:34
Sign up to request clarification or add additional context in comments.

Comments

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

answered Jun 7, 2020 at 4:39

2 Comments

I am getting UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined at Array.reduce (<anonymous>)
never mind it worked the array was declared empty first after I filled it in I got the right data
2
const sampleOut = response.filter(response => directories.includes(response.directories[0]))
 .map(response => response.id);
answered Jun 7, 2020 at 4:33

Comments

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)

answered Jun 7, 2020 at 4:51

Comments

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)

answered Jun 7, 2020 at 4:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.