如何查看 javascript 中对象内的值是否相同 "line"
How to see if values are in the same "line" within an object in javascript
不确定如何解决这个问题,但我想看看两个值是否相同 'line'
var inventory_needed = [
{ section: "hardware", supplies: "hammers" },
{ section: "plumbing", supplies: "pipes" },
{ section: "garden", supplies: "grass seeds" },
{ section: "cleaning supplies", supplies: ["hand sanitizer", "detergent"] },
{ section: "appliances", supplies: ["fridges", "dishwashers"] }
];
我想尝试的伪代码
if(section.value && supplies.value in the same line) {
return true;
}
else {
return false;
}
//example 1
if("appliances" && "fridges" in the same line) {
return true; //would return true
}
else {
return false;
}
//example 2
if("plumbing" && "fridges" in the same line) {
return true;
}
else {
return false; //would return false
}
在同一行中,您的意思似乎是在数组中的同一对象内定义。如果那是正确的,方法是这样的:
function inTheSameLine(section, supplies){
return inventory_needed.some(obj => {
return obj.section === section && (
obj.supplies === supplies || (
Array.isArray(obj.supplies) && obj.supplies.includes(supplies)
)
);
});
}
JavaScript数组some
函数returns如果数组中的任何一个元素满足条件则为真
不确定如何解决这个问题,但我想看看两个值是否相同 'line'
var inventory_needed = [
{ section: "hardware", supplies: "hammers" },
{ section: "plumbing", supplies: "pipes" },
{ section: "garden", supplies: "grass seeds" },
{ section: "cleaning supplies", supplies: ["hand sanitizer", "detergent"] },
{ section: "appliances", supplies: ["fridges", "dishwashers"] }
];
我想尝试的伪代码
if(section.value && supplies.value in the same line) {
return true;
}
else {
return false;
}
//example 1
if("appliances" && "fridges" in the same line) {
return true; //would return true
}
else {
return false;
}
//example 2
if("plumbing" && "fridges" in the same line) {
return true;
}
else {
return false; //would return false
}
在同一行中,您的意思似乎是在数组中的同一对象内定义。如果那是正确的,方法是这样的:
function inTheSameLine(section, supplies){
return inventory_needed.some(obj => {
return obj.section === section && (
obj.supplies === supplies || (
Array.isArray(obj.supplies) && obj.supplies.includes(supplies)
)
);
});
}
JavaScript数组some
函数returns如果数组中的任何一个元素满足条件则为真