我有一个" N" Objets
,我需要做的是仅恢复或保留同一数组中的Objets
或其他newField
与所有Objects
的价值相同的数据Array
Array
的。{注意:新的Array
或最终的 export class BucketPipe implements PipeTransform {
public arr = []
transform(value: any, args?: any): any {
return this.seleccion(value);
}
seleccion(value) {
console.log("val: " +JSON.stringify(value))
var newValue = new Array()
for(var i = 0; i < value.length; i++) {
for(var y = 0; y < value[i].length; y++) {
if(value[i][y]) {
var val = value[i][y]
do {
newValue.push(value[i][y])
val = value[i][y]
} while (value[i][y].newField !== val.newField)
}
}
}
return this.filter(newValue)
}
filter(values) {
var field: string[] = new Array()
for(var i = 0; i < values.length; i++) {
Object.keys(values[i]).map((obj,key) => {
if(values[i].newField == values[i][obj]) field.push(values[i])
})
}
this.arr = field
var result = [];
this.arr.forEach((subArr, index) => {
if(this.validate(subArr, index))
result.push(subArr);
});
console.log("Result: ",result)
}
validate(subArr, index) {
var test = true;
for(var i = 0; i < subArr.length && test; i++){
var obj = subArr[i];
var subtest = false;
for(var j = 0; j < this.arr.length && !subtest; j++){
if(index == j) continue;
var found = this.arr[j].find(function(e){
return e["newField"] == obj["newField"];
});
if(found) subtest = true;
}
test = test && subtest;
}
return test;
}
}
每个重复元素只能有一个Object。
注意2:我必须编辑它,因为返回值必须在所有对象中都是通用的。例如,如果具有相同newField的3/3对象将返回一个,如果具有相同newField的2/3对象则不返回任何内容
这就是我所拥有的,但它真的没什么......:/
(这是来自Angular 2的过滤器) 我会感激任何帮助。
谢谢
[[{"id":42,
"idBucket":"patrimonial",
"originalField":"c",
"newField":"nCliente",
"name":"Número Cliente",
"description":"Número del Cliente",
"filter":true,
"visible":true,
"idDataType":1},{"id":43,
"idBucket":"patrimonial",
"originalField":"ct",
"newField":"nContrato",
"name":"Número Contrato",
"description":"Número de Contrato",
"filter":true,
"visible":true,
"idDataType":1},{"id":45,
"idBucket":"patrimonial",
"originalField":"s",
"newField":"sucursal",
"name":"Sucursal",
"description":"Sucursal",
"filter":true,
"visible":true,
"idDataType":1
},{"id":47,
"idBucket":"patrimonial",
"originalField":"sp",
"newField":"sProducto",
"name":"Subproducto",
"description":"Subproducto",
"filter":true,
"visible":true,
"idDataType":1
}],
[{
"id":11,
"idBucket":"expunic",
"originalField":"nc",
"newField":"nCliente",
"name":"Número Cliente",
"description":"Número del Cliente",
"filter":true,
"visible":true,
"idDataType":1
},{
"id":12,
"idBucket":"expunic",
"originalField":"t",
"newField":"titulo",
"name":"Título Aplicación",
"description":"Título de la Aplicación o Gaveta",
"filter":true,
"visible":true,
"idDataType":1
}],
[{
"id":16,
"idBucket":"bastanteo",
"originalField":"t",
"newField":"titulo",
"name":"Título Aplicación",
"description":"Nombre de la Gaveta",
"filter":true,
"visible":true,
"idDataType":1
}]]
这是数组的一个例子:
newField
在此示例中,不会返回任何内容,因为常见的 [[{
"id":11,
"idBucket":"expunic",
"originalField":"nc",
"newField":"nCliente",
"name":"Número Cliente",
"description":"Número del Cliente",
"filter":true,
"visible":true,
"idDataType":1
},{
"id":12,
"idBucket":"expunic",
"originalField":"t",
"newField":"titulo",
"name":"Título Aplicación",
"description":"Título de la Aplicación o Gaveta",
"filter":true,
"visible":true,
"idDataType":1
}],
[{
"id":16,
"idBucket":"bastanteo",
"originalField":"t",
"newField":"titulo",
"name":"Título Aplicación",
"description":"Nombre de la Gaveta",
"filter":true,
"visible":true,
"idDataType":1
}]]
值为&#34; titulo&#34;但它只在阵列位置1和2中但不在0中,如果是newField值&#34; titulo&#34;在所有3中它将返回一个只有一个Object的新数组,并不关心其中一个3.
另一个例子
Object
在此示例中,它将返回一个newField
,因为在所有2个对象中,Objects
具有相同的值,并不关心其中之一。虽然如果阵列中有10个Objets,则所有10个newField
必须具有相同的Objects
值。请注意,只有在newFields
Objects
个值时,才能返回2 <div ng-init="addEditRuleSetCtrl.initSelectors()">
<query-builder group="addEditRuleSetCtrl.filter.group"
rule-condition-operators="addEditRuleSetCtrl.ruleConditionOperators"
rule-condition-set-operators="addEditRuleSetCtrl.ruleConditionSetOperators"
fields="addEditRuleSetCtrl.fieldCodes"
></query-builder>
</div>
以上self.initSelectors = function()
{
self.getRuleConditionOperators();
self.getRuleConditionSetOperators();
self.fieldCodes = self.getCorrespondingFields();
console.log("initSelectors : fieldCodes: " + JSON.stringify(self.fieldCodes ));
}
self.getCorrespondingFields = function()
{
return self.origin == 'DDE' ? self.fieldsDDE : self.fieldsATT;
};
答案 0 :(得分:0)
这是一种可行的方法:
function getFirstObjectForEachRepeatedKey(values, keyName)
{
if (!keyName)
keyName = "newField";
var haveSeenKey = {};
var alreadySelectedValue = {};
var result = [];
for (var i = 0; i < values.length; i++)
{
var key = values[i][keyName];
if (!(key in haveSeenKey))
haveSeenKey[key] = values[i];
else
{
var selectedValue = haveSeenKey[key];
if (selectedValue !== alreadySelectedValue)
{
result.push(selectedValue);
haveSeenKey[key] = alreadySelectedValue;
}
}
}
return result;
}
答案 1 :(得分:0)
这就是我的理解。 (我用你的阵列测试了它并给出了正确的结果):
// define your array here (must be named arr or you can modify the code accordingly
var arr = [[{}, {}, {}], [{}], [{}, {}]]; // ...
function validate(subArr, index){
var test = true;
for(var i = 0; i < subArr.length && test; i++){
var obj = subArr[i];
var subtest = false;
for(var j = 0; j < arr.length && !subtest; j++){
if(index == j) continue;
var found = arr[j].find(function(e){
return e["newField"] == obj["newField"];
});
if(found) subtest = true;
}
test = test && subtest;
}
return test;
}
var result = [];
arr.forEach(function(subArr, index){
if(validate(subArr, index))
result.push(subArr);
});
代码段:
var arr = [[{
"id": 42,
"idBucket": "patrimonial",
"originalField": "c",
"newField": "nCliente",
"name": "Número Cliente",
"description": "Número del Cliente",
"filter": true,
"visible": true,
"idDataType": 1
}, {
"id": 43,
"idBucket": "patrimonial",
"originalField": "ct",
"newField": "nContrato",
"name": "Número Contrato",
"description": "Número de Contrato",
"filter": true,
"visible": true,
"idDataType": 1
}, {
"id": 45,
"idBucket": "patrimonial",
"originalField": "s",
"newField": "sucursal",
"name": "Sucursal",
"description": "Sucursal",
"filter": true,
"visible": true,
"idDataType": 1
}, {
"id": 47,
"idBucket": "patrimonial",
"originalField": "sp",
"newField": "sProducto",
"name": "Subproducto",
"description": "Subproducto",
"filter": true,
"visible": true,
"idDataType": 1
}],
[{
"id": 11,
"idBucket": "expunic",
"originalField": "nc",
"newField": "nCliente",
"name": "Número Cliente",
"description": "Número del Cliente",
"filter": true,
"visible": true,
"idDataType": 1
}, {
"id": 12,
"idBucket": "expunic",
"originalField": "t",
"newField": "titulo",
"name": "Título Aplicación",
"description": "Título de la Aplicación o Gaveta",
"filter": true,
"visible": true,
"idDataType": 1
}],
[{
"id": 16,
"idBucket": "bastanteo",
"originalField": "t",
"newField": "titulo",
"name": "Título Aplicación",
"description": "Nombre de la Gaveta",
"filter": true,
"visible": true,
"idDataType": 1
}]];
function validate(subArr, index){
var test = true;
for(var i = 0; i < subArr.length && test; i++){
var obj = subArr[i];
var subtest = false;
for(var j = 0; j < arr.length && !subtest; j++){
if(index == j) continue;
var found = arr[j].find(function(e){
return e["newField"] == obj["newField"];
});
if(found) subtest = true;
}
test = test && subtest;
}
return test;
}
var result = [];
arr.forEach(function(subArr, index){
if(validate(subArr, index))
result.push(subArr);
});
console.log(result);