0

Given the following array of objects:

var data = [
  {fruit: "apples", stock: false, season: true},
  {fruit: "peaches", stock: true, season: false},
  {fruit: "oranges", stock: false, season: false},
  {fruit: "pears", stock: false, season: true},
]

and these two arrays:

var fruits = ["apples", "peaches"]
var inv = ["stock"]

How can I filter the objects, from data, so that objects are kept:

  • if they have a fruit in the fruits array; AND
  • they have a property from the inv array which is set to true

So, in the above example, only peaches survive:

  var result = [
    {fruit: "peaches", stock: true, season: false}
  ]
2
  • The simplest approach would be three loops, hence... What have you tried so far? Commented Sep 19, 2018 at 17:08
  • what means "they have a property from the inv array which is set to true"? does a single property has to be set to true, or if more all? Commented Sep 19, 2018 at 17:12

4 Answers 4

3

I think you can just use filter with some() and includes():

var data = [
  {fruit: "apples", stock: false, season: true},
  {fruit: "peaches", stock: true, season: false},
  {fruit: "oranges", stock: false, season: false},
  {fruit: "pears", stock: false, season: true},
]

var fruits = ["apples", "peaches"]
var inv = ["stock"]

let filtered = data.filter(item => 
    fruits.includes(item.fruit) && inv.some(i => item[i]))

console.log(filtered)

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

Comments

2

You could filter by looking in the fruits array with Array#includes and check the property by iterating inv with Array#every.

var data = [ { fruit: "apples", stock: false, season: true }, { fruit: "peaches", stock: true, season: false }, { fruit: "oranges", stock: false, season: false }, { fruit: "pears", stock: false, season: true }],
    fruits = ["apples", "peaches"],
    inv = ["stock"],
    result = data.filter(o => fruits.includes(o.fruit) && inv.every(k => o[k]));
   
console.log(result);

3 Comments

It seems your should use some instead of the every: "they have a property from the inv array which is set to true".
this part is questionable.
Not a big deal anyways. OP can swap the methods according to his/her needs.
0

You can use combination of filter and every here.

var data = [
  {fruit: "apples", stock: false, season: true},
  {fruit: "peaches", stock: true, season: false},
  {fruit: "oranges", stock: false, season: false},
  {fruit: "pears", stock: false, season: true},
]

var fruits = ["apples", "peaches"]
var inv = ["stock"]

var result= data.filter(a=> fruits.some(b=> b == a.fruit) && inv.every(k => a[k]));
console.log(result)

Comments

0
var output = data.filter(val => {
    if (fruits.includes(val.fruit)) {
        return inv.filter(prop => {
            if (val[prop] == true)
                return val
        }).length > 0
    }
})
console.log(output)

I think this will do, if you just want to filter through.

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.