根据另一个对象数组中的过滤条件来过滤对象数组:JavaScript

时间:2019-04-25 14:47:37

标签: javascript arrays object search filter

我希望根据myArray中提到的条件过滤myFilter。 myFilter的键已定义,可以使用myFilter.fieldmyFilter.value进行访问,其中myArray的key:value未知。 我们可能必须遍历myArray中的每个对象,才能首先将myArray [key]与myFilter.field匹配,然后再将myArray [key]匹配到myFilter.value。

那应该是AND逻辑

myArray = [{
    make: "Honda",
    model: "CRV",
    year: "2017"
  },
  {
    make: "Toyota",
    model: "Camry",
    year: "2020"
  },
  {
    make: "Chevy",
    model: "Camaro",
    year: "2020"
  }
]

myFilter = [{
    field: "make",
    value: "Chevy",
    type: "string"
  },
  {
    field: "year",
    value: "2020",
    type: "date"
  }
];


// Expected OutPut:
myArray = [{
  make: "Chevy",
  model: "Camaro",
  year: "2020"
}]


var tempArray = [];
const keysToMatch = myFilter.length;
let matchedItems = [];
myArray.forEach((data) => {
  matchedItems = [];
  let itemsToFind = Object.values(data);
  myFilter.forEach((filterItem) => {
    if (itemsToFind.indexOf(filterItem.value) != -1) {
      matchedItems.push("matched");
    }
  });
  //check if everything matched
  if (matchedItems.length === keysToMatch) {
    tempArray.push(data);
  }
});

console.log(tempArray);

4 个答案:

答案 0 :(得分:0)

您可以对循环和数组过滤器使用normal。定义变量filteredArray并使用for循环迭代myFilter。在每次迭代期间,创建一个变量k,其值将设置为过滤后的数组。因此,第一步,k的初始值为myArray,将过滤k,并将过滤后的值设置为filteredArray。在第二次迭代期间,依此类推,k的值将设置为第一个过滤后的数组

let myArray = [{
    make: "Honda",
    model: "CRV",
    year: "2017"
  },
  {
    make: "Toyota",
    model: "Camry",
    year: "2020"
  },
  {
    make: "Chevy",
    model: "Camaro",
    year: "2020"
  }
]

let myFilter = [{
    field: "make",
    value: "Chevy",
    type: "string"
  },
  {
    field: "year",
    value: "2020",
    type: "date"
  }
];
let filteredArray;

for (let i = 0; i < myFilter.length; i++) {
  let k = filteredArray !== undefined ? filteredArray : myArray
  if (myFilter[i].field === 'make') {
    filteredArray = k.filter(item => item[myFilter[i].field] === myFilter[i].value)
  } else if (myFilter[i].field === 'year') {
    filteredArray = k.filter(item => item[myFilter[i].field] === myFilter[i].value)
  }

}
console.log(filteredArray)

答案 1 :(得分:0)

var tempArray = [];
const keysToMatch = myFilter.length;
let matchedItems = [];
myArray.forEach((data) => {
    matchedItems = [];
    let itemsToFind = Object.values(data);
    myFilter.forEach((filterItem) => {
        if (itemsToFind.indexOf(filterItem.value) != -1) {
            matchedItems.push("matched");
        }
    });
    //check if everything matched
    if (matchedItems.length === keysToMatch) {
        tempArray.push(data);
    }
});

console.log(tempArray);

答案 2 :(得分:0)

从理论上讲这应该起作用,但是由于某种原因不起作用(返回一个空数组)。我希望其他读者能够提高自己的水平!随时进行编辑。

myArray = [{
    make: "Honda",
    model: "CRV",
    year: "2017"
  },
  {
    make: "Toyota",
    model: "Camry",
    year: "2020"
  },
  {
    make: "Chevy",
    model: "Camaro",
    year: "2020"
  }
]

myFilter = [{
    field: "make",
    value: "Chevy",
    type: "string"
  },
  {
    field: "year",
    value: "2020",
    type: "date"
  }
];


const myNewArray = myArray.filter((car) => myFilter.every((filter) => car[filter.field] === car[filter.value]));

console.log(myNewArray);

答案 3 :(得分:0)

根据您的需要,这可能有些复杂,但是可以。

myArray = [
    {
        make: "Honda", 
        model: "CRV",
        year: "2017"
    },
    {
        make: "Toyota", 
        model: "Camry",
        year: "2020"},
    {    
        make: "Chevy", 
        model: "Camaro",
        year: "2020"}
    ]

    myFilter = [
        {
            field: "make",
            value: "Chevy",
            type: "string"
        },
        {
            field: "year",
            value: "2020",
            type: "date"
        }
    ];
         //only return those that return true
var newArray = myArray.filter(car => {
	var temp = true;
    //iterate over your filters
	for (var i = 0; i < myFilter.length; i++) {
        //if any filters result in false, then temp will be false
		if (car[myFilter[i].field] != myFilter[i].value) {
			temp = false;
		}
	}
	if (temp == true) {
		return true;
	} else {
		return false;
	}
});
console.log(JSON.stringify(newArray));