Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
Get an array of strings from an array of objects with conditions

What is the best way to get an array of strings from an array of objects, where you can specify to only take value x where value y=z?

Current solution:

    array = [{
        "Item": "A",
        "Quantity": 2
      },
      {
        "Item": "B",
        "Quantity": 7
      },
      {
        "Item": "C",
        "Quantity": 7
      },
      {
        "Item": "D",
        "Quantity": 7
      },
      {
        "Item": "E",
        "Quantity": 7
      },
      {
        "Item": "F",
        "Quantity": 1
      }
    ];
    
    let filteredValues = array.map((el) => el.Quantity === 7 && el.Item);
    
    console.log(filteredValues)

Expected outcome:

["B", "C", "D", "E"]

Actual outcome:

[false, "B", "C", "D", "E", false]

Additional info: using next.js / react

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

First, do filter and then do a map to get only the property you need.

const array = [
  { Item: "A", Quantity: 2 },
  { Item: "B", Quantity: 7 },
  { Item: "C", Quantity: 7 },
  { Item: "D", Quantity: 7 },
  { Item: "E", Quantity: 7 },
  { Item: "F", Quantity: 1 },
];

let filteredValues = array
  .filter((el) => el.Quantity === 7 && el.Item)
  .map(({ Item }) => Item);

console.log(filteredValues);

Or, you can use reduce as below.

const array = [
  { Item: "A", Quantity: 2 },
  { Item: "B", Quantity: 7 },
  { Item: "C", Quantity: 7 },
  { Item: "D", Quantity: 7 },
  { Item: "E", Quantity: 7 },
  { Item: "F", Quantity: 1 },
];

let filteredValues = array.reduce((results, el) => {
  if (el.Quantity === 7 && el.Item) {
    results.push(el.Item);
  }
  return results;
}, []);

console.log(filteredValues);

about 4 years ago · Juan Pablo Isaza Report

0

The best way is to use Array.prototype.reduce

let data = [{
    "Item": "A",
    "Quantity": 2
  },
  {
    "Item": "B",
    "Quantity": 7
  },
  {
    "Item": "C",
    "Quantity": 7
  },
  {
    "Item": "D",
    "Quantity": 7
  },
  {
    "Item": "E",
    "Quantity": 7
  },
  {
    "Item": "F",
    "Quantity": 1
  }
];

const result = data.reduce((accumulator, current) => {
  return current["Quantity"] === 7 ? accumulator.concat(current["Item"]): accumulator;
}, [])

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

Use Array.filter() along with Array.map()

Working Demo :

const array = [{
  "Item": "A",
  "Quantity": 2
},             {
  "Item": "B",
  "Quantity": 7
},             {
  "Item": "C",
  "Quantity": 7
},{
  "Item": "D",
  "Quantity": 7
},{
  "Item": "E",
  "Quantity": 7
},{
  "Item": "F",
  "Quantity": 1
}];

const filteredValues = array.filter((el) => el.Quantity === 7).map(elem => elem.Item);
    
console.log(filteredValues)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!