Is there any way to check and compare value of array of array with the name
only and print the matched value and unmatched value separately?
arrayToCheck =[
{
id: 1,
name:Java,
description: language
},
{
id: 2,
name:JavaScript,
description: language
},
{
id: 3,
name:Oops,
description: Subject
},
{
id: 4,
name:Java,
description: Practical
},
{
id: 5,
name:Oops,
description: Practical
},
{
id: 6,
name:React Js,
description: Practical
},
]
valueToCheck= JavaScript;
Expected Output:
matchedValue = JavaScript
unMatchedValue = ['Java', 'Oops','React Js']; //filter repeated data
First- Your array was made incorrectly.
It should have been like this.
var arrayToCheck =[
{
id: 1,
name:"Java",
description: "language",
},
{
id: 2,
name:"JavaScript",
description: "language",
},
{
id: 3,
name:"Oops",
description: "Subject",
},
{
id: 4,
name:"Java",
description: "Practical",
},
{
id: 5,
name:"Oops",
description: "Practical",
},
{
id: 6,
name:"React Js",
description: "Practical",
},
]
You should also have a filter before all your code. Like this-
var filter = {
name: "JavaScript"
};
Now you should have something like this.
var filter = {
name: "JavaScript"
};
var arrayToCheck =[
{
id: 1,
name:"Java",
description: "language",
},
{
id: 2,
name:"JavaScript",
description: "language",
},
{
id: 3,
name:"Oops",
description: "Subject",
},
{
id: 4,
name:"Java",
description: "Practical",
},
{
id: 5,
name:"Oops",
description: "Practical",
},
{
id: 6,
name:"React Js",
description: "Practical",
},
]
Now to use the filter and log it in the console.
var filter = { name: "JavaScript" }; var arrayToCheck =[ { id: 1, name:"Java", description: "language", }, { id: 2, name:"JavaScript", description: "language", }, { id: 3, name:"Oops", description: "Subject", }, { id: 4, name:"Java", description: "Practical", }, { id: 5, name:"Oops", description: "Practical", }, { id: 6, name:"React Js", description: "Practical", }, ] arrayToCheck= arrayToCheck.filter(function(item) { for (var key in filter) { if (item[key] === undefined || item[key];= filter[key]) return false; } return true; }). console.log(arrayToCheck)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.