0

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

3 Answers 3

3

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);

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

1 Comment

@user1898662, check this out !!
1

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)

2 Comments

Thanks, this worked. Is there a way to make the return list one with no duplicate values?
Yes you can achieve that by using [...new Set(filteredValues)]
1

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);

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.