Filter array with multiple conditions

I am having an array of objects and I would like to filter them on multiple conditions. Either if the element.state === false or the element.url contains a certain or multiple strings.

I tried to filter with a certain string however, I get an empty array back. Find below my example:

const attr = ['Product 1', 'Product 2']

const res = []
for (let i = 0; i < attr.length; i++) {
  res.push({
    attr: attr[i],
    url: 'category/',
    state: false,
  }, {
    attr: attr[i],
    url: 'costs/',
    state: false,
  }, {
    attr: attr[i],
    url: 'ownership/',
    state: false,
  }, {
    attr: attr[i],
    url: 'earnings/',
    state: false,
  }, {
    attr: attr[i],
    url: 'price/',
    state: false,
  })
}

/* console.log(JSON.stringify(res))
 */

function setState(obj, bool) {
  obj.state = bool
}

function getState(res) {
  return res.state
}

setState(res[1], true)

const para = 'category'
let currentState = res.filter((el, para) => {
  el.state === false && el.url === para
})

console.log(`Initial state: ${currentState.length}`)
while (currentState.length > 0) {
  currentState[0].state = true
  currentState = res.filter(el => el.state === false)
  console.log(`Set state to true for ${JSON.stringify(currentState[0])}`)
  console.log(currentState.length)
}

console.log('###################DONE#####################')


/*
 * ################
 * #Wanted Output for 'category' and 'ownership'
 * ################
 * {
 * attr: 'Product 1',
 * url: 'category/',
 * state: true
 * },
 * {
 * attr: 'Product 2',
 * url: 'category/',
 * state: true
 * },
 * {
 * attr: 'Product 1',
 * url: 'ownership/',
 * state: true
 * },
 * {
 * attr: 'Product 2',
 * url: 'ownership/',
 * state: true
 * }
 */

Any suggestions what I am doing wrong?

I appreciate your replies!

Answers:

Answer

Several problems:

  1. No return in filter()

  2. The urls end in / so won't equal the para string that doesn't also end in /

  3. Your filter() is using parameter name para also which shadows the para variable outside it

const para = 'category'
let currentState = res.filter((el) => {
  return el.state === false && el.url.startsWith(para)
})

console.log(currentState)
<script>
  const attr = ['Product 1', 'Product 2']
  const res = []
  for (let i = 0; i < attr.length; i++) {
    res.push({
      attr: attr[i],
      url: 'category/',
      state: false,
    }, {
      attr: attr[i],
      url: 'costs/',
      state: false,
    }, {
      attr: attr[i],
      url: 'ownership/',
      state: false,
    }, {
      attr: attr[i],
      url: 'earnings/',
      state: false,
    }, {
      attr: attr[i],
      url: 'price/',
      state: false,
    })
  }
</script>

Answer

You're setting para = 'category' but the url is 'category/' so the filtering returns nothing.

Also, in the function:

let currentState = res.filter((el) => { // <- removed the second element "para" because it will hold the index of el in res, which is not what you intended.
  el.state === false && el.url.indexOf(para) > -1; // check if it's a substring
})

The name para is misleading: the second parameter is the index of the element, and it's "hiding" the constant para that you declared outside.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.