Reputation: 446
I currently have an array that looks like this:
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
I want to create a function that takes 2 values, and returns a new array containing the objects that match those two values.
var name = "a";
var age = 1;
someFunction(name, age);
And returns something that looks like this:
newArray = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
I have tried using the filter method and the reduce methods but no success. If someone could help me or point me in the right direction, I would greatly appreciate that.
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
var result = array.filter(function(obj) {
return obj.name === "a" && obj.age === 1;
});
return result[0];
}
console.log(result);
Upvotes: 1
Views: 58
Reputation: 16908
In the Array#filter
callback, just compare the name
& age
of current element with those of the params you passed.
var array = [{"name":"a","age":1,"siblings":3},{"name":"b","age":3,"siblings":5},{"name":"a","age":1,"siblings":2}]
var name = "a";
var age = 1;
function someFunction(name, age){
return array.filter((obj) => obj.name === name && obj.age === age)
}
console.log(someFunction(name, age));
You can also use Array#reduce
. Just add the matching object in the accumulator array and return it.
var array = [{"name":"a","age":1,"siblings":3},{"name":"b","age":3,"siblings":5},{"name":"a","age":1,"siblings":2}]
var name = "a";
var age = 1;
function someFunction(name, age){
return array.reduce((acc, obj) => {
if(obj.name === name && obj.age === age)
acc.push(obj);
return acc;
}, []);
}
console.log(someFunction(name, age));
Upvotes: 1
Reputation: 630
Try this
let array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
];
function someFunction(name, age) {
let newArray = [];
array.forEach(function(arr) {
let nameMatch = false;
let ageMatch = false;
let obj = arr;
for(key in obj) {
if(obj.hasOwnProperty(key)) {
if(key === 'name') {
// console.log('name key i am', key);
let nameValue = obj[key];
if(nameValue === name) {
nameMatch = true;
// console.log('name match', nameValue, name);
}
} else if(key === 'age') {
let ageValue = obj[key];
if(ageValue === age) {
// console.log('age match', ageValue, age);
ageMatch = true
}
}
}
}
if(ageMatch && nameMatch) {
// console.log('true');
newArray.push(arr);
// console.log(arr);
}
});
return newArray;
}
let name = "a";
let age = 1;
let myArr = someFunction(name, age);
console.log('My Arr', myArr);
Upvotes: 0
Reputation: 12152
Use the filter function
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
var name = "a";
var age = 1;
someFunction(name, age);
function someFunction(name,age)
{
console.log(array.filter((e)=>e.name==name && e.age==age?true:false
))}
Upvotes: 0
Reputation: 2039
here is your working function
function someFunction (name, age) {
return array.filter(val => val.name == name && val.age == age)
}
Upvotes: 1
Reputation: 386604
You could take an array with the wanted key/value pair for searching.
Take Array#filter
with a check for the predicates with Array#every
.
var array = [{ name: "a", age: 1, siblings: 3 }, { name: "b", age: 3, siblings: 5 }, { name: "a", age: 1, siblings: 2 }],
search = [['name', 'a'], ['age', 1]],
result = array.filter(object => search.every(([key, value]) => object[key] === value));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1