Nick D
Nick D

Reputation: 630

Javascript check if string exists in array of objects

I have two arrays in my program, one contains a list of strings that act as keys, the other contains objects that contain a key as well.

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];

I need to return another array that contains objects like classesInList only if the key (as the className property) exists in classKeys

For example, in this scenario, I would need to check the classKeys against the classesInList and create an array with two items :

[{key : "WIHUGDYVWJ", className : 'math'},{key : "likubqwd", className : 'robotics'}]

since english is not in the classKeys, it is deleted from the classesInList. How would I go about this?

Side Note: I am using react native so I have access to es6 properties and functions.

I am completely stuck in this sense. No matter how simple this seems to be I just cannot find an answer.

Upvotes: 4

Views: 17940

Answers (5)

jo_va
jo_va

Reputation: 13963

You can do this in ES6 using filter, includes and object destructuring:

const classKeys = ['math', 'robotics'];
const classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];

const result = classesInList.filter(({ className }) => classKeys.includes(className));

console.log(result);

Upvotes: 0

Jack Bashford
Jack Bashford

Reputation: 44107

Use destructuring with Array.prototype.filter and Array.prototype.includes:

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'}];

let filtered = classesInList.filter(({ className }) => classKeys.includes(className));

console.log(filtered);

Upvotes: 0

Prince Hernandez
Prince Hernandez

Reputation: 3721

using filter will filter your array, using some to get if some of the values is truthy for your criteria and finally using includes to check if the value is included in your classKeys.

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'},];


const result = classesInList
               .filter(c => 
                  Object.values(c).some(val => 
                                        classKeys.includes(val)
                  )
                );

console.log(result);

Upvotes: 0

Terry
Terry

Reputation: 66123

Just use Array.prototype.filter and combine that with Array.prototype.includes. What you want to do is to filter the classesInList array of objects, and only select those whose className value are present in the classKeys array.

let classKeys = ['math', 'robotics'];
let classesInList = [{key : "WIHUGDYVWJ", className : 'math'},{key : "qwljdhkuqwdnqwdk", className : 'english'},{key : "likubqwd", className : 'robotics'}];

let filteredClasses = classesInList.filter(cls => classKeys.includes(cls.className));

console.log(filteredClasses);

Upvotes: 6

Tholle
Tholle

Reputation: 112787

You can use the filter array method and check if the element's className is in the classKeys array:

let classKeys = ["math", "robotics"];
let classesInList = [
  { key: "WIHUGDYVWJ", className: "math" },
  { key: "qwljdhkuqwdnqwdk", className: "english" },
  { key: "likubqwd", className: "robotics" }
];

let result = classesInList.filter(element => {
  return classKeys.indexOf(element.className) !== -1;
});

console.log(result);

Upvotes: 1

Related Questions