英文:
Filter array of objects based on other array of objects
问题
我试图从对象optionData的字段数组中过滤出相应的字体颜色:
{
"fields": [
{
"type": "color",
"id": "font-color",
"value": "#222",
"target": "font",
"dependency": []
},
{
"type": "color",
"id": "font-color",
"value": "#f00",
"target": "font",
"dependency": []
}
],
"textposition": {
"left": "12",
"top": "10"
},
"textsubtitle": "Kleur",
"additionalcosts": "4,99"
}
我有一个包含依赖关系的依赖数组。在这种情况下,它仅包含字体颜色和2个值;#fff 和 #222。因此,我想要过滤出 #222。
依赖关系:
[
{
"field": "font-color",
"value": "#fff"
},
{
"field": "font-color",
"value": "#222"
}
]
const optionData = {
"fields": [
{
"type": "color",
"id": "font-color",
"value": "#222",
"target": "font",
"dependency": []
},
{
"type": "color",
"id": "font-color",
"value": "#f00",
"target": "font",
"dependency": []
}
],
"textposition": {
"left": "12",
"top": "10"
},
"textsubtitle": "Kleur",
"additionalcosts": "4,99"
};
console.log(optionData, 'optionData');
const dependency = [
{
"field": "font-color",
"value": "#fff"
},
{
"field": "font-color",
"value": "#222"
}
];
console.log(dependency, 'dependency');
let filtered = optionData['fields'].filter(field => {
console.log(field, 'field');
return dependency.filter(dependency => {
console.log(dependency, 'dependency')
return dependency['field'] !== field['id'] && dependency['value'] !== field['value'];
})
})
console.log(filtered, 'filtered');
请注意,上述代码是在JavaScript中的示例,用于过滤出与依赖数组中的字体颜色不匹配的字段。
英文:
I'm trying to filter out the corresponding font-colors from the field array in the object optionData:
{
"fields": [
{
"type": "color",
"id": "font-color",
"value": "#222",
"target": "font",
"dependency": []
},
{
"type": "color",
"id": "font-color",
"value": "#f00",
"target": "font",
"dependency": []
}
],
"textposition": {
"left": "12",
"top": "10"
},
"textsubtitle": "Kleur",
"additionalcosts": "4,99"
}
I have a dependency array where I keep the dependencies. In this case it consists only of font-colors and 2 values; #fff and #222. Therefore I want to filter out the #222.
Depencies:
[
{
"field": "font-color",
"value": "#fff"
},
{
"field": "font-color",
"value": "#222"
}
]
const optionData = {"fields":[{"type":"color","id":"font-color","value":"#222","target":"font","dependency":[]},{"type":"color","id":"font-color","value":"#f00","target":"font","dependency":[]}],"textposition":{"left":"12","top":"10"},"textsubtitle":"Kleur","additionalcosts":"4,99"};
console.log(optionData, 'optionData');
const dependency = [{"field":"font-color","value":"#fff"},{"field":"font-color","value":"#222"}];
console.log(dependency, 'dependency');
let filtered = optionData['fields'].filter(field => {
console.log(field, 'field');
return dependency.filter(dependency => {
console.log(dependency, 'dependency')
return dependency['field'] !== field['id'] && dependency['value'] !== field['value'];
})
})
console.log(filtered, 'filtered');
答案1
得分: 1
以下是翻译好的部分:
这段代码使用了filter方法来迭代optionData['fields']数组,并仅保留满足过滤条件的对象。条件是使用dependency数组上的some方法实现的。它检查是否至少有一个对象在dependency中与optionData['fields']中当前对象的field和value匹配。! 运算符对结果进行否定,以便只保留没有匹配依赖项的对象。
在提供的示例中,filtered数组将仅包含value为"#f00"的对象,因为它与dependency数组中的任何依赖项都不匹配。
英文:
Try this:
const optionData = {
"fields": [
{
"type": "color",
"id": "font-color",
"value": "#222",
"target": "font",
"dependency": []
},
{
"type": "color",
"id": "font-color",
"value": "#f00",
"target": "font",
"dependency": []
}
],
"textposition": {
"left": "12",
"top": "10"
},
"textsubtitle": "Kleur",
"additionalcosts": "4,99"
};
const dependency = [
{"field": "font-color", "value": "#fff"},
{"field": "font-color", "value": "#222"}
];
let filtered = optionData['fields'].filter(field => {
return !dependency.some(dependency => {
return dependency['field'] === field['id'] && dependency['value'] === field['value'];
});
});
console.log(filtered);
The code uses the filter method to iterate over the optionData['fields'] array and keep only the objects that satisfy the filtering condition. The condition is implemented using the some method on the dependency array. It checks if there is at least one object in dependency that matches both the field and value of the current object in optionData['fields']. The ! operator negates the result so that only objects that do not have a matching dependency are kept.
In the provided example, the filtered array will contain only the object with value: "#f00" since it does not match any of the dependencies in the dependency array
答案2
得分: 1
filter
使用一个布尔值(条件的结果)来确定是否应过滤元素,然而你的内部 filter
返回一个新数组,所以并不是特别合适。
还有一个数组方法,你可以使用它来返回布尔值:some
。对于每个迭代的字段,你可以检查依赖数组中相应条目是否匹配。如果有匹配项,该方法会立即中断,以避免必须遍历整个依赖数组。
const options = {
fields: [
{ type: "color", id: "font-color", value: "#222", target: "font", dependency: [] },
{ type: "color", id: "font-color", value: "#f00", target: "font", dependency: [] }
],
textposition: { left: "12", top: "10" },
textsubtitle: "Kleur",
additionalcosts: "4,99"
};
const dependencies = [
{ field: "font-color", value: "#fff" },
{ field: "font-color", value: "#222" }
];
function filterDependencies(options, dependencies) {
return {
...options,
fields: options.fields.filter(field => {
return !dependencies.some(q => {
return (
q.field === field.id &&
q.value === field.value
);
});
})
}
}
console.log(filterDependencies(options, dependencies));
附加文档:
英文:
filter
uses a boolean (the result of a condition) to determine whether an element should be filtered or not, however your inner filter
returns a new array so it's not really suitable.
There is another array method that you can use that does return a boolean: some
. For each iterated field you can check to see if some
of the corresponding entries in the dependencies array are a match. If there is a match the method immediately breaks so that the whole dependency array doesn't have to be iterated over.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const options={fields:[{type:"color",id:"font-color",value:"#222",target:"font",dependency:[]},{type:"color",id:"font-color",value:"#f00",target:"font",dependency:[]}],textposition:{left:"12",top:"10"},textsubtitle:"Kleur",additionalcosts:"4,99"},dependencies=[{field:"font-color",value:"#fff"},{field:"font-color",value:"#222"}];
function filterDependencies(options, dependencies) {
// Return a new object from (spreading out) the
// old object, and creating a new fields array by
// filtering out any fields that are in the dependecies
// array
return {
...options,
fields: options.fields.filter(field => {
// For each iterate field if `some` (ie any) of the
// corresponding dependency array fields are a match
// If there is a match return false, otherwise true.
// Use that boolean as your `filter` condition
return !dependencies.some(q => {
return (
q.field === field.id
&& q.value === field.value
);
});
})
}
}
console.log(filterDependencies(options, dependencies));
<!-- end snippet -->
Additional documentation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论