如何获取对象数组中的键?
how to get to the key in an array of objects?
希望你一切都好
我想问一些(我希望)基本的问题,我有这个函数负责返回过滤后的对象,该对象具有转换为颜色或大小的特定“键”变量。
好吧,我将颜色和大小变量放在一个对象数组中,我想知道我现在必须在我的“项目[键]”中使用什么术语才能获得我的“颜色” " 变量如上图所示
picture showing what key im able to get now and then what key im looking to get!
在此先感谢您提供的所有帮助,祝您有愉快的一天!
这里是这个过程中使用的两个函数的代码:
const [filtros,setFiltros] = useState({});
const gerirFiltros = (evento) =>{
const valor = evento.target.value;
console.log(evento.target.name + evento.target.value)
if (evento.target.name === "cor" ) {
const cor = evento.target.name
setFiltros( {
...filtros,
["variacoes"]:[{
[evento.target.name]:valor
}],
})
}
else {
setFiltros({
...filtros,
[evento.target.name]:valor,
}) // THIS IS JUST TO PASS TO PAGE #2 (https://pastebin.com/4GH3Mi3H) THE VARIABLE `filtros` THAT IS AN ARRAY WITH MANY FILTERS LIKE -> {marca:"Paz rodrigues"}, etc..
以及接收过滤器的函数(我认为我需要更改的那个):
useEffect(() => {
categoria &&
setProdutosFiltrados(
produtos.filter((item) =>
Object.entries(filtros).every(([key,value],i) =>
//console.log("key ->" + key + "value->" + value[0].cor) )
item[key].includes(value)
)
)
)
您可以使用some()
useEffect(() => {
categoria &&
setProdutosFiltrados(
produtos.filter((item) =>
Object.entries(filtros).every(([key,value],i) =>{
//Here the value is an array 'variacoes' so to check colors use filter to get all the elements of 'variacoes' array;
//Also assuming that the color you are passing will be available here as item[key]
var allColors = item.map(i=>i.cor)
return value.some((val)=>allColors.includes(val.cor))
}
)
)
)
希望你一切都好
我想问一些(我希望)基本的问题,我有这个函数负责返回过滤后的对象,该对象具有转换为颜色或大小的特定“键”变量。
好吧,我将颜色和大小变量放在一个对象数组中,我想知道我现在必须在我的“项目[键]”中使用什么术语才能获得我的“颜色” " 变量如上图所示
picture showing what key im able to get now and then what key im looking to get!
在此先感谢您提供的所有帮助,祝您有愉快的一天!
这里是这个过程中使用的两个函数的代码:
const [filtros,setFiltros] = useState({});
const gerirFiltros = (evento) =>{
const valor = evento.target.value;
console.log(evento.target.name + evento.target.value)
if (evento.target.name === "cor" ) {
const cor = evento.target.name
setFiltros( {
...filtros,
["variacoes"]:[{
[evento.target.name]:valor
}],
})
}
else {
setFiltros({
...filtros,
[evento.target.name]:valor,
}) // THIS IS JUST TO PASS TO PAGE #2 (https://pastebin.com/4GH3Mi3H) THE VARIABLE `filtros` THAT IS AN ARRAY WITH MANY FILTERS LIKE -> {marca:"Paz rodrigues"}, etc..
以及接收过滤器的函数(我认为我需要更改的那个):
useEffect(() => {
categoria &&
setProdutosFiltrados(
produtos.filter((item) =>
Object.entries(filtros).every(([key,value],i) =>
//console.log("key ->" + key + "value->" + value[0].cor) )
item[key].includes(value)
)
)
)
您可以使用some()
useEffect(() => {
categoria &&
setProdutosFiltrados(
produtos.filter((item) =>
Object.entries(filtros).every(([key,value],i) =>{
//Here the value is an array 'variacoes' so to check colors use filter to get all the elements of 'variacoes' array;
//Also assuming that the color you are passing will be available here as item[key]
var allColors = item.map(i=>i.cor)
return value.some((val)=>allColors.includes(val.cor))
}
)
)
)