1

Here I have two arrays array1 and array2 I want to find ids from array1 which array1's names matched with array2 values how to find ids in javascript ?

array1:

[ {id: 1, name: "Hindi"}
  {id: 2, name: "English"}
  {id: 3, name: "French"}
  {id: 4, name: "Russian"}
  {id: 5, name: "Urdu"}
  {id: 6, name: "Japanese"}
]

array2:

["Hindi", "Russian", "Urdu"]

I tried this code

console.log(array1.find(x => x.name === array2).id;
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
Dharmesh
  • 5,383
  • 17
  • 46
  • 62
  • 2
    What would you expect from e.g. `"Hindi" === ["Hindi", "Russian", "Urdu"]`? Look into filtering and mapping arrays. – jonrsharpe May 02 '19 at 07:12
  • Do you expect an *array* of id values as output? – user202729 May 02 '19 at 07:13
  • @jonrsharpe no "Hindi" === "Hindi" , "Russian" === "Russian", "Urdu" === "Urdu" and want that ids in array like this [1, 4, 5] – Dharmesh May 02 '19 at 07:14
  • That is **not** what you've written. Try decomposing this into smaller steps, e.g. how to check whether a given value is *in* (not *equal to*) an array. – jonrsharpe May 02 '19 at 07:17
  • Possible duplicate of [Simplest code for array intersection in javascript](https://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – George May 02 '19 at 07:23

4 Answers4

1

You can use filter() to get objects whose names are in the array.Then use map() to convert array of values to array of ids.

In your code you are comparing the string with array x.name === array2. You should use includes()

let arr = [ {id: 1, name: "Hindi"}, {id: 2, name: "English"}, {id: 3, name: "French"}, {id: 4, name: "Russian"}, {id: 5, name: "Urdu"}, {id: 6, name: "Japanese"} ]
let lang = ["Hindi", "Russian", "Urdu"];

let res = arr.filter(x => lang.includes(x.name)).map(x => x.id);
console.log(res)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • @DDD Consider accepting the answer if it works for you. I don't know why I got a downvote eventhough the code works according to need of OP. – Maheer Ali May 02 '19 at 07:24
1

You should use filter method in combination with map and destructuring

let arr1 = [ {id: 1, name: "Hindi"}, {id: 2, name: "English"}, {id: 3, name: "French"}, {id: 4, name: "Russian"}, {id: 5, name: "Urdu"}, {id: 6, name: "Japanese"} ], arr2 = ["Hindi", "Russian", "Urdu"];

console.log(arr1.filter(({name}) => arr2.includes(name)).map(({id}) => id));
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

Try this:

 var a = [{
      id: 1,
      name: "Hindi"
    }, {
      id: 2,
      name: "English"
    }, {
      id: 3,
      name: "French"
    }, {
      id: 4,
      name: "Russian"
    }, {
      id: 5,
      name: "Urdu"
    }, {
      id: 6,
      name: "Japanese"
    }]
    var b = ["Hindi", "Russian", "Urdu"];
    var c = a.filter(function(i){
    return b.indexOf(i.name)>-1;
    });
    console.log(c); // New Array with filtered values
Sudhir Ojha
  • 3,247
  • 3
  • 14
  • 24
K K
  • 17,794
  • 4
  • 30
  • 39
0

This will work too :)

var a1 = [{
  id: 1,
  name: "Hindi"
}, {
  id: 2,
  name: "English"
}, {
  id: 3,
  name: "French"
}, {
  id: 4,
  name: "Russian"
}, {
  id: 5,
  name: "Urdu"
}, {
  id: 6,
  name: "Japanese"
}]
var a2 = ["Hindi", "Russian", "Urdu"];
var filter = a1.filter(function(i) {
  return a2.indexOf(i.name) > -1;
}).map(function(obj) {
  return obj.id;
});;
console.log(filter);
p u
  • 1,395
  • 1
  • 17
  • 30