10
I have the following objects:
{ letter: "A", num1: "1", num2: "2", num3: "3" }
{ letter: "B", num1: "3", num2: "2", num3: "1" }
{ letter: "C", num1: "2", num2: "3", num3: "1" }
I would like to transform into a single object that takes letters as attribute and a number array/object value:
var unicoObjeto = {
A: { num1: "1", num2: "2", num3: "3" },
B: { num1: "3", num2: "2", num3: "1" },
C: { num1: "2", num2: "3", num3: "1" }
};
What I got so far was:
var items = {};
var indice = [];
var data = [
{ letter: "A", num1: "1", num2: "2", num3: "3" },
{ letter: "B", num1: "3", num2: "2", num3: "1" },
{ letter: "C", num1: "2", num2: "3", num3: "1" }
];
$.each(data, function (i, val) {
indice.push(val.letter);
});
Object.keys(data).forEach(key => {
let newKey = indice[key];
items[newKey] = items[key];
});
console.log(items);
Which presents the following (I don’t know how to pass the values now):
{ A: undefined, B: undefined, C: undefined }
In Javascript (no jQuery required): Function getUnicoObject(vector) { const unicoObject = {}; vector.foreach(Function(e) { const novoObjeto = {}; const attributes = Object.Keys(e).filter(a => a != "Letter"); attributes.foreach(a => newObject[a] = e[a]); unicoObject[e.Letter] = newObject; }); Return unicoObject; }
– user158926