Home > other >  compare Two array and result only the values
compare Two array and result only the values

Time:11-20

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"]

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

try this

CodePudding user response:

You can also use filter and then map

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

const arrayTwo = [33, 193];

var result = arrayOne.filter((itm) => arrayTwo.includes(itm.id)).map(prop => prop.name);

console.log(result);
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

this function is your solution:

const findNames = () => {
let names = []
arrayOne.forEach(item => {
    if (arrayTwo.includes(item.id)) {
        names = [...names, item.name]
     }
   })
  return names
}

CodePudding user response:

Here is your compare() function for getting array of names based on two array

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

const arrayTwo = [33, 157];

function compare(arrayOne, arrayTwo) {
    let result = [];
    arrayOne.filter(element => arrayTwo.indexOf(element.id) !== -1 ).forEach(element => { result.push(element.name) });
    return result ;
}

CodePudding user response:

Use a flatmap on the first array and include only those members having an id property included in array two.

arrayOne.flatMap(({ id, name }) => arrayTwo.includes(id) ? [name]: []);

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

const arrayTwo = [33, 193];

const result = arrayOne.flatMap(({ id, name }) => arrayTwo.includes(id) ? [name]: []);

console.log(result);
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related