3

I have an array a = ["APP","COM", "REJ" , "COM APP"] and b = [23,54,56,24]. A and B are of equal length. How to get all elements of array B, in which the values of array a contains VALUE as APP

here in the array a there is an APP in APP ,COM APP , how to return [23, 24] as an array.

3 Answers 3

5

Use Array#filter method along with String#indexOf method.

var a = ["APP", "COM", "REJ", "COM APP"],
  b = [23, 54, 56, 24],
  c = 'APP';


console.log(
  b.filter(function(v, i) {
    return a[i].indexOf(c) > -1;
  })
)


For exact word match then use RegExp#test method with word boundary regex .

var a = ["APP", "COM", "REJ", "COM APP"],
  b = [23, 54, 56, 24],
  c = /\bAPP\b/;


console.log(
  b.filter(function(v, i) {
    return c.test(a[i]);
  })
)

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

Comments

1

you can use tis code:

var a = ["APP", "COM", "REJ", "COM APP"],
b = [23, 54, 56, 24],
var find=[]
for(var key in b){
  if(a[key].indexOf('APP')!=-1)
    find.push(b[key])
}
console.log(find)

Comments

0

You may also do as follows;

var a = ["APP","COM", "REJ" , "COM APP"],
    b = [23,54,56,24],
    r = b.reduce((p,c,i) => a[i].includes("APP") ? p.concat(c) : p ,[]);
console.log(r);

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.