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

95
Views
compare Two array and result only the values

I need to filter an array of objects

const arrayOne = [
    {id: 33, name: "fruit"},
    {id: 157, name: "car"},
    {id: 193, name: "water"},
];

const arrayTwo = [33, 193];

I need only the names in an array (without the key)

Expected output

["fruit", "water"]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

1) You can easily achive the result using Map

const arrayOne = [
  { id: 33, name: "fruit" },
  { id: 157, name: "car" },
  { id: 193, name: "water" },
];
const map = new Map();
arrayOne.forEach((o) => map.set(o.id, o.name));

const arrayTwo = [33, 193];
const result = arrayTwo.map((o) => map.get(o));

console.log(result);

2) You can also achieve the result using map and find

const arrayOne = [
  { id: 33, name: "fruit" },
  { id: 157, name: "car" },
  { id: 193, name: "water" },
];

const arrayTwo = [33, 193];
const result = arrayTwo.map((id) => arrayOne.find((o) => o.id === id)?.name);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

you can just simply do

const arrayOne = [
  { id: 33, name: "fruit" },
  { id: 157, name: "car" },
  { id: 193, name: "water" }
];

const arrayTwo = [33, 193];
let keepResult = [];
arrayOne.map((a1) => {
  arrayTwo.map((a2) => {
    if (a1.id === a2) {
      keepResult.push(a1.name);
    }
  });
});

console.log("result show", keepResult);

about 4 years ago · Juan Pablo Isaza Report

0

const arrayOne = [
    {id: 33, name: "fruit"},
    {id: 157, name: "car"},
    {id: 193, name: "water"},
];

const arrayTwo = [33, 193];

let newArray = arrayOne.filter(item => arrayTwo.includes(item.id)).map(item => item.name)

console.log(newArray);

try this

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!