2017-12-04 19 views
0

の配列を操作:は、統合/ Iは次のようにオブジェクトの配列を持つオブジェクト

[{ 
    animal: "cat", 
    dateString: "2017-01-03" 
},{ 
    animal: "dog", 
    dateString: "2017-02-05" 
},{ 
    animal: "cat", 
    dateString: "2017-03-04" 
}] 

と私は次のようになり、新しい配列を作成したいと思います:どのようにすることができ

[{ 
    animal: "cat", 
    values: [ 
    "2017-01-03", 
    "2017-03-04" 
    ] 
},{ 
    animal: "dog", 
    values: [ 
    "2017-02-05" 
    ] 
}] 

を個々のオブジェクトのプロパティを値の配列に統合しますか?

+1

配列が機能を削減すること –

答えて

2

lodash/RAMDAがなければuはあなたが唯一のJSをタグ付けしていないので

const a = [ 
    { 
    animal: 'cat', 
    dateString: '2017-01-03' 
    }, { 
    animal: 'dog', 
    dateString: '2017-02-05' 
    }, { 
    animal: 'cat', 
    dateString: '2017-03-04' 
    } 
] 

const b = Object.values(a.reduce((a, b) => { 
    if (a[b.animal]) { 
    a[b.animal].values.push(b.dateString) 
    } else { 
    a[b.animal] = {animal: b.animal, values: [b.dateString]} 
    } 
    return a 
}, {})) 
+0

vscodeは「Object.values」で次の文句ポピュラーな方法です。 [TS]プロパティ 'values'は型 'ObjectConstructor'には存在しません。 – Simpler

+0

nm。私はこれを "(オブジェクト)"に変更しなければならなかったので、typescriptは不平を言っていませんでした。 – Simpler

0

を行うことができます、あなたがソートあなたの配列を倍増するforEachメソッドを使用することができ

var x = [{ 
     animal: "cat", 
     dateString: "2017-01-03" 
    },{ 
     animal: "dog", 
     dateString: "2017-02-05" 
    },{ 
     animal: "cat", 
     dateString: "2017-03-04" 
    }]; 

    let usedKeys = []; 
    let finalArray = []; 

    for(var z = 0; z < x.length ; z++) 
    { 
     if(usedKeys.indexOf(x[z]['animal']) == '-1') 
     { 
      usedKeys.push(x[z]['animal']); 
      finalArray.push({'animal':x[z]['animal'] , 'values':[x[z]['dateString']]}); 
     } 
     else 
     { 
      let targetIndex = usedKeys.indexOf(x[z]['animal']); 
      finalArray[targetIndex]['values'].push(x[z]['dateString']); 
     } 
    } 

console.log(finalArray); 
+0

JS Fiddle Link:https://jsfiddle.net/9ph4zett/ – Varun

0

この(バニラJS)をしてみてください。

var arr = [{ 
    animal: "cat", 
    dateString: "2017-01-03" 
},{ 
    animal: "dog", 
    dateString: "2017-02-05" 
},{ 
    animal: "cat", 
    dateString: "2017-03-04" 
}]; 

var sorted = []; 

arr.forEach(function callback(arrVal, index1, array1) { 
    sorted.forEach(function callback(sortVal, index2, array2) { 
     if (arrVal == sortVal) { 
     sorted[index2].values.push(arrVal.dateString); 
     } else { 
     sorted.push({animal: arr[index1].animal, values: [arr[index1].dateString]}); 
     } 
    }); 
}); 

希望します。あなたは値が動物の名前に基づいてグループ化される新しいオブジェクトで、あなたのオブジェクトを格納するためにarray#reduceを使用することができます

+0

ネストされたForEachループは、大規模配列のパフォーマンス上の問題にはなりませんか? – Simpler

+0

大きな意味のものによって異なります。この場合、パフォーマンスは、第1の並べ替えられていない配列の分散に大きく依存します。パフォーマンスを向上させたい場合は、ソートされたデータを一度に取得するためにデータベースクエリを調べる必要があります。 –

0

var data=[{ 
 
    animal: "cat", 
 
    dateString: "2017-01-03" 
 
},{ 
 
    animal: "dog", 
 
    dateString: "2017-02-05" 
 
},{ 
 
    animal: "cat", 
 
    dateString: "2017-03-04" 
 
}] 
 

 
var ans = data.reduce(function(accumulator, element) { 
 
var elementInAccumulator = accumulator[element.animal] 
 
if(elementInAccumulator) { 
 
    if(Array.isArray(elementInAccumulator.values)) { 
 
    elementInAccumulator.values.push(element.dateString) 
 
    } 
 
    else { 
 
    var arr=[] 
 
    
 
    arr.push(elementInAccumulator.dateString) 
 
    arr.push(element.dateString) 
 
    elementInAccumulator.values= arr 
 
    delete(elementInAccumulator.dateString) 
 
    } 
 
} 
 
else { 
 
    accumulator[element.animal] = element 
 
} 
 
return accumulator 
 
}, {}) 
 
console.log(Object.keys(ans).map(function(element) { 
 
    return ans[element] 
 
}))

0

。次に、Object.valuesを使用して結果を取得します。ここで

var data = [{ animal: "cat", dateString: "2017-01-03" },{ animal: "dog", dateString: "2017-02-05" },{ animal: "cat", dateString: "2017-03-04" }]; 
 

 
var result = data.reduce((o, {animal, dateString}) => { 
 
    o[animal] ? o[animal].values.push(dateString) : o[animal] = {animal, values: [dateString]} 
 
    return o; 
 
},{}); 
 

 
var output = Object.values(result); 
 
console.log(output);

0

まず新しい、空の配列とfor-loopを作成します。そこからは、動物の種類のとブランドの新しいオブジェクトを作成したり、ちょうどそれがない場合は、他のdateString値をプッシュし、次のいずれか

const animals = [{ 
 
    animal: "cat", 
 
    dateString: "2017-01-03" 
 
}, { 
 
    animal: "dog", 
 
    dateString: "2017-02-05" 
 
}, { 
 
    animal: "cat", 
 
    dateString: "2017-03-04" 
 
}]; 
 

 
const combined = []; 
 

 
for (let i = 0; i < animals.length; i++) { 
 
    const { animal, dateString } = animals[i]; 
 
    
 
    const animalIndex = combined.findIndex(obj => { 
 
    return obj['animal'] === animal; 
 
    }); 
 
    
 
    if (animalIndex === -1) { 
 
    combined.push({ animal, values: [dateString] }); 
 
    } else { 
 
    combined[animalIndex]['values'].push(dateString); 
 
    } 
 
} 
 

 
console.log(combined);

0

を私たちは単純に配列を反復処理し、その結果を構築することができます各動物をその特性を含む対象にマッピングすることによって、例えば、 {'cat': {'animal': 'cat', 'values': ["2017-01-03"]}}。我々が完了したら、我々は値のみを抽出します:

const arr = [{ 
 
    animal: "cat", 
 
    dateString: "2017-01-03" 
 
},{ 
 
    animal: "dog", 
 
    dateString: "2017-02-05" 
 
},{ 
 
    animal: "cat", 
 
    dateString: "2017-03-04" 
 
}] 
 

 
let res= {} 
 
arr.forEach(obj => { 
 
    if (res[obj.animal]) { 
 
    res[obj.animal].values.push(obj.dateString) 
 
    } else { 
 
    res[obj.animal] = {animal: obj.animal, values: [obj.dateString]} 
 
    } 
 
}) 
 

 
res = Object.values(res) 
 
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; }

関連する問題