我目前有一个看起来像这样的数组:
var array = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "b",
"age": 3,
"siblings": 5
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
我想创建一个具有2个值的函数,并返回一个包含与这两个值匹配的对象的新数组。
var name = "a";
var age = 1;
someFunction(name, age);
并返回如下所示的内容:
newArray = [
{
"name": "a",
"age": 1,
"siblings": 3
},
{
"name": "a",
"age": 1,
"siblings": 2
}
]
我尝试使用filter方法和reduce方法,但没有成功。如果有人可以帮助我或指出正确的方向,我将不胜感激。
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);
答案 0 :(得分:1)
您可以将包含所需键/值对的数组用于搜索。
以Array#filter
的谓词作为对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; }
答案 1 :(得分:1)
这是您的工作功能
function someFunction (name, age) {
return array.filter(val => val.name == name && val.age == age)
}
答案 2 :(得分:1)
在Array#filter
回调中,只需将当前元素的name
和age
与您传递的参数进行比较。
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));
您也可以使用Array#reduce
。只需将匹配的对象添加到累加器数组中并返回即可。
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));
答案 3 :(得分:0)
使用过滤器功能
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
))}
答案 4 :(得分:0)
尝试
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);