1

I have an array like this :

 const appoint =[
  { a: "asas",
    b:{au: false, h:false,l:true}
  },
  { a: "avd",
    b:{au: true, h:false,l:true}
  },
  { a: "as", b:{au: true, h:false,l:false}
  }];

When I access b , I want to filter the falsy values, so far I'm not successful doing it with a serie of map() like this :

const result = appoint.map(elem => elem.b.)
const finalresult = result.map(
         item =>item.filter(
              (item, value)=> item[value] === false )
          )
6
  • elem.b. <-- is the last point a typo?... Anyway, you can't apply filter on an object. What is the expected output format? Commented Jun 28, 2019 at 13:15
  • how the expected output will look like? Commented Jun 28, 2019 at 13:16
  • To elaborate, you want to remove any properties that have false as value, from the related objects? Or create a new object, that has all properties, except for the ones with false as value? As a note, imho, the name finalresult is not good, if the previous one is not at least intermediateresult. Next, you'll add a finalfinalresult or an endresult, and readability only goes downhill here. Commented Jun 28, 2019 at 13:18
  • This issue is that you are mapping an array then filtering an object, and objects cannot be filtered Commented Jun 28, 2019 at 13:19
  • the outcome should be this: [l,l,au] Commented Jun 28, 2019 at 13:20

4 Answers 4

1

You can first use map to get an new array which contains only b, then use reduce and inside reduce callback use for..in to iterate the object and get the keys which is true

const appoint = [{
    a: "asas",
    b: {
      au: false,
      h: false,
      l: true
    }
  },
  {
    a: "avd",
    b: {
      au: true,
      h: false,
      l: true
    }
  },
  {
    a: "as",
    b: {
      au: true,
      h: false,
      l: false
    }
  }
];


let filtered = appoint.map(function(item) {
  return item.b;
}).reduce(function(acc, curr) {
  for (let keys in curr) {
    if (curr[keys]) {
      acc.push(keys)
    }
  }

  return acc;
}, []);
console.log(filtered)

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

Comments

1

elem.b is an object, not an array, so you can't use filter on it. You could do something like this:

const bArray = appoint.map(elem => elem.b)
const finalresult = bArray.map(b => {
  bKeys = Object.keys(b)
  const filtered = {}
  bKeys.forEach(key => {
    if (!b[key]) filtered[key] = false
  })
  return filtered
})

3 Comments

this is helpful, what about if I want filtred to be an array ?
An array of what? Key names that have a false value? const filtered = []; bKeys.forEach(key => { if (!b[key]) filtered.push(key) })
Or const filtered = bKeys.filter(key => !b[key])
1

You can create a getKeysBy() function that takes an object, and a predicate and returns the keys that pass the predicate check.

Now you can use Array.flatMap() with getKeysBy() to get an array of all the keys.

const appoint = [{"a":"asas","b":{"au":false,"h":false,"l":true}},{"a":"avd","b":{"au":true,"h":false,"l":true}},{"a":"as","b":{"au":true,"h":false,"l":false}}]
  
const getKeysBy = (predicate, obj) => 
  Object.entries(obj)
  .filter(([, v]) => predicate(v))
  .map(([k]) => k)
  
const result = appoint.flatMap(o => getKeysBy(v => v !== false, o.b))

console.log(result)

Comments

0

You can do something like this

let array = [{
    a: "asas",
    b: {
        au: false,
        h: false,
        l: true
    }
}, {
    a: "avd",
    b: {
        au: true,
        h: false,
        l: true
    }
}, {
    a: "as",
    b: {
        au: true,
        h: false,
        l: false
    }
}, {
    a: "ab",
    b: {
        au: false,
        h: false,
        l: false
    }
}]

let output = array.filter((element) => {
    let keys = Object.keys(element.b)
    element.b = keys.filter((key) => {
        if (element.b[key]) {
            return key
        }
    })
    if (!Object.keys(element.b).length) {
        element.b = []
    }
    return element;
})
console.log(output)   

This will give you

[
    {
        "a": "asas",
        "b": [
            "l"
        ]
    },
    {
        "a": "avd",
        "b": [
            "au",
            "l"
        ]
    },
    {
        "a": "as",
        "b": [
            "au"
        ]
    },
    {
        "a": "ab",
        "b": []
    }
]

I assume you might need an empty array if none of the values are true.

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.