0

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.

7 Answers 7

1

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)

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

3 Comments

I do like your answer but it does not return an array of objects, I guess I could accommodate
I tested both Nina's (which nail it the result) and your answer, and this one is more versatile to my needs
I have another question, what if I need to add the index of the test values as well, for example { "1": "B - 0" }, { "3": "D - 1" }, where 0 and 1 are the index of B and D respectively in the test array, hope that makes sense
1

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

2 Comments

ok, but how about test = [A B E H missing-val]?
@georg, is that requested?
0

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

Comments

0

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

Comments

0

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;}, 
                             [])

Comments

0

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.