Ricardo Sanchez
Ricardo Sanchez

Reputation: 5167

Get array of objects as result of comparing two arrays?

Given 2 arrays of different length:

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];

I need a new array containing the values in 'vals' that are in 'test' and their corresponding index as an object like

result = [{1:"B"}, {3:"D"}, {4:"E"}, {7:"H"}]

Where {1:"B"} represents the object with index from the original array as key 1 and the matching value B

I got as far as

const result = [vals, test];
result.reduce((a, b) => a.filter(c => b.includes(c)));

That yields ["B", "D", "E", "H"] But I am struggling to get the index as well.

Upvotes: 0

Views: 70

Answers (7)

georg
georg

Reputation: 214969

How about this?

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];


result = [...vals.entries()]
    .filter(e => test.includes(e[1]))
    .map(e => ({ [e[0]]: e[1] }))
  
console.log(result)

Here's a variation that computes indexes in both arrays and returns an array of triples value, vals-index, test-index, which you can convert as you please:

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];


result = [...vals.entries()]
    .map(e => [e[1], e[0], test.indexOf(e[1])] )
    .filter(e => e[2] >= 0)
  
console.log(result)

Upvotes: 1

tinku kalluri
tinku kalluri

Reputation: 63

  vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
  test = ["B", "D", "E", "H"]
  ans=list()
  k=0
  for i,j in enumerate(vals):
    if(k<len(test)):
       if(j==test[k]):
          ans.append({i,j})
           k=k+1
 print(ans)

Upvotes: 0

egx
egx

Reputation: 399

This should work:

const ArrayOne = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]; const ArrayTwo = ["B", "D", "E", "H"];

let result = ArrayOne.map((v, i) => [i, v]).filter(p => ArrayTwo.includes(p[1]))

console.log(result)

Then you can just convert the array into array of objects afterwards if needed.

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386600

You could map with Array#indexOf and computed property names.

const
    vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
    test = ["B", "D", "E", "H"],
    result = test.map(v => ({ [vals.indexOf(v)]: v }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Rajdeep D
Rajdeep D

Reputation: 3910

You can achieve this using reduce method

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];

let result = vals.reduce((acc,i, index) => { 
                                    let obj = {}; 
                                   if(test.includes(i)){
                                       obj[index]=i;
                                       acc.push(obj);
                                   }
               
                                   return acc;}, 
                             [])

Upvotes: 0

Dibash Sapkota
Dibash Sapkota

Reputation: 665

You could check this out

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];

const result=test.map(function(r){
  let i=vals.indexOf(r);
  let obj={};
  obj[i]=r;
  return obj;
})

console.log(result);
//result = [{1:"B"}, {3:"D"}, {4:"E"}, {7:"H"}]

Upvotes: 0

dave
dave

Reputation: 64657

The third argument to reduce is the index, so you could just do:

const vals = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
const test = ["B", "D", "E", "H"];

const result = vals.reduce((carry, current, index) => {
    if (test.includes(current)) {
        carry.push({[index]: current});
    }
    return carry;
}, []);

console.log(result);

Upvotes: 0

Related Questions