JavaScript Get array of keys based on values from another array | JS

Hello say i have an array of objects that looks like this

let myArray = [
  {item1: true},
  {item2: false},
  {item3: true},
  {item4: false}
]

How would I iterate though this to return a new array of true values that looks like this:

let newArray = ['item1', 'item3']

I found this function but it only returns single items:

function findKey(map, term) {
  var found = [];
  for(var property in map) {
    if(map.hasOwnProperty(property)) {
      for(var key in map[property]) {
        if(map[property].hasOwnProperty(key) && key === term) {
          found.push(property);
        }
      }
    }
  }
  return found;
}

Thanks!

Answer:1

Assuming myArray always contains objects with only 1 property.

let newArray = myArray
    .map(item => Object.entries(item)[0])
    .filter(([key, value]) => value)
    .map(([key, value]) => key)
Answer:2

Use the function reduce to build the desired output. The handler of the function reduce will get the keys and check for each value === true.

This approach checks for the whole set of keys within an object. Further, this way you only use one loop.

let myArray = [{item1: true},{item2: false},{item3: true},{item4: false}],
    result = myArray.reduce((a, c) => a.concat(Object.keys(c).filter(k => c[k] === true)), []);
    
console.log(result);
Answer:3

Something much optimized than the accepted answer would look like this:

const arr = [
  { item1: true },
  { item2: false },
  { item3: true },
  { item4: false }
]

const result = [];
const len = arr.length;

for (let i = 0; i < len; ++i) {
  const obj = arr[i];
  const key = Object.keys(obj)[0];

  if(obj[key]) {
    result.push(key);
  }
}

console.log(result);

There is only one loop over the array, instead of map and filter which ends up looping twice.

Answer:4

I have a problem. I just started learning jQuery, my first experience with javascript at all. And I want to make an element animate on click, and I did make that work: HTML: <h1>Testing</h1&...

I have a problem. I just started learning jQuery, my first experience with javascript at all. And I want to make an element animate on click, and I did make that work: HTML: <h1>Testing</h1&...

  1. jquery multiple click events
  2. jquery multiple click handlers
  3. jquery multiple click selectors
  4. jquery multiple click events same function
  5. jquery multiple click
  6. jquery multiple click events firing
  7. jquery multiple click functions
  8. jquery multiple click events order
  9. jquery multiple click listeners
  10. jquery multiple click handlers order
  11. jquery multiple click classes
  12. jquery click multiple elements
  13. jquery click multiple times
  14. jquery click multiple elements same class
  15. jquery click multiple buttons
  16. jquery click multiple id
  17. jquery click multiple elements same id
  18. jquery click multiple div
  19. jquery click multiple parameters
  20. jquery click multiple arguments

const map = {} for (let i=0;i<10**5;i++) { map[i] = true } let ans = 0 for (let i in map) { for (let j in map) { ans += i+j } } console.log(ans) The above code when run ...

const map = {} for (let i=0;i<10**5;i++) { map[i] = true } let ans = 0 for (let i in map) { for (let j in map) { ans += i+j } } console.log(ans) The above code when run ...

  1. javascript heap memory error
  2. javascript heap memory increase
  3. javascript heap memory limit
  4. javascript heap memory
  5. js heap memory
  6. js heap memory leak
  7. javascript memory heap stack
  8. javascript memory heap code
  9. clear javascript heap memory
  10. javascript heap out memory
  11. javascript memory heap size
  12. javascript heap out of memory
  13. firefox javascript heap memory disclosure vulnerability
  14. node js heap memory usage
  15. node js heap memory
  16. javascript heap out of memory npm
  17. javascript heap out of memory webpack
  18. javascript heap out of memory node
  19. javascript heap out of memory npm build
  20. javascript heap out of memory angular 7

I'm using RSpec + capybara, and the capybara-webkit as driver. I have to check if a JS box exists in the page after clicking on a button, but with no results. If I use selenium as a driver, the test ...

I'm using RSpec + capybara, and the capybara-webkit as driver. I have to check if a JS box exists in the page after clicking on a button, but with no results. If I use selenium as a driver, the test ...

  1. check exists in sql
  2. check file exists with javascript
  3. check if file exists with python
  4. check if file exists with wildcard bash
  5. check if file exists with powershell
  6. check if element exists with jquery
  7. check if file exists with curl
  8. check if file exists with javascript
  9. check if file exists with bash
  10. check if service exists with ansible
  11. check if file exists with ifstream
  12. check if class exists with jquery
  13. check if file exists with vba
  14. check if file exists with wget
  15. check if file exists with php
  16. check if row exists with mysql
  17. check if user exists with ansible
  18. check if directory exists with powershell
  19. check if folder exists with powershell

I have a div in which I have a button and a textarea. What is the easiest way to get my hands on a child from a given div by its id? <html> <head> <link rel="stylesheet" ...

I have a div in which I have a button and a textarea. What is the easiest way to get my hands on a child from a given div by its id? <html> <head> <link rel="stylesheet" ...

  1. element same line css
  2. element same id html
  3. element same number of protons and neutrons
  4. element same
  5. element same atom
  6. element same as molecule