2

I have an array in JavaScript like

var input =['p1',  'p3'];

And my data is like below

var data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}

I need to filter the data object based on the input array. Is there a simple ES6 method to pull it out.

Tia

6 Answers 6

4

You can simply start from your input array and map to your data:

var input =['p1', 'p3'];
var data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}

const result = input.map(x => data[x])

console.log(result)

As long as you do not need to traverse the data tree.

If you need recursively to traverse the object tree you could try something like this:

var input = ['p1', 'p3', 'p4', 'pp'];
var data = {
  p: {
    p1: 'A'
  },
  pa: {
    pb: {
      p3: 'B'
    }
  },
  p4: 'C'
}

// recursivly flatten object keys
const flatten = (obj, a=[]) => Object.entries(obj)
  .reduce((r,[k,v]) => (typeof v == 'object' ? flatten(v,a) : r.push(k), r), a)

// search flattened objects
const searchKeys = (d, t) => 
  d.filter(x => flatten(x).some(x => x.toLowerCase().includes(t.toLowerCase())))

// Break to array of objects
const arr = Object.entries(data).reduce((r,[k,v]) => (r.push({[k]:v}), r), [])

console.log(input.map(x => searchKeys(arr, x)))

Sign up to request clarification or add additional context in comments.

Comments

2

Here is a snippet that might help you:

const data = {
  p1: {
    name: 'test1',
    value: 'value1'
  },
  p2: {
    name: 'test2',
    value: 'value2'
  },
  p3: {
    name: 'test3',
    value: 'value3'
  }
}
const input = ['p1', 'p2']

console.log(input.map(key => data[key]))

this is a simple way to do as per your expectation:

var data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}
var input=['p1','p2']

console.log(Object.keys(data).map(obj => {
  return input.includes(obj) && data[obj] 
}))

Comments

1

you can try this

let input =['p1',  'p3'];
let data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}

let res = input.map(x =>  ({[x]:data[x]})) 
console.log(res)

output

(2) […]
   0: Object { p1: {…} }​
   1: Object { p3: {…} }

Comments

1

If by filter object, you mean you want an object with keys that are in the input array, you can reduce your input array to that object:

var input = ['p1', 'p3'];
var data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}

var res = input.reduce((a, c) => {
  a[c] = data[c];
  return a;
}, {});
console.log(res);

Comments

0

Here's a way to filter that using forEach

var input =['p1',  'p3'];
var data = {
      p1: { name: 'test1', value:'value1'},
      p2: { name : 'test2', value:'value2'}, 
      p3: { name: 'test3', value: 'value3'}
}

input.forEach(filter => {
    // Check if this key exists in data
    if (data[filter]) {
         console.log(data[filter])
    }
})

Comments

0

Filtering the object based on keys present in the array

var input =['p1',  'p3'];

var data = {p1: { name: 'test1', value:'value1'}, p2: { name : 'test2', value:'value2'}, p3: { name: 'test3', value: 'value3'}}


   var filtered= Object.keys(data)
          .filter( key => input.includes(key))
          .reduce( (res, key) => Object.assign(res, { [key]: data[key] }), {} );
console.log(filtered);

Comments

Your Answer

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.