嵌套对象转换为数组,并放入一个对象中。

huangapple go评论66阅读模式
英文:

Nested Objects into Arrays into an Object

问题

I would need help to access the value of a key-value pair from an object that is itself nested into an array (several objects with 2 key-value pairs inside an array Several arrays into an object).

So for example, I would need to access only one of the names such as Max or Lucas...

I tried to access it but no luck... Any help would be much appreciated.

const nested = {
    40: [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    50: [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};


// Here is what I tried but I didn't find any way to access a console.log that would return only a // single in the output.


const keysHello = Object.keys(nested);
console.log("keysHello", keysHello); // ['40', '50']

const values = Object.values(nested);
console.log("values", values); // [Array(4), Array(4)]

const keysValues = Object.entries(nested);
console.log("keysValues", keysValues); // [['40', Array(4)], ['50', Array(4)]]

// The one below does not work
// const [, , {name}] = nested;
// console.log(`${Object.values[40]}`);
英文:

I would need help to access the value of a key-value pair from an object that is itself nested into an array (several objects with 2 key-value pairs inside an array Several arrays into an object).

So for example, I would need to access only one of the names such as Max or Lucas...

I tried to access it but no luck... Any help would be much appreciated.

const nested = {
    40: [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    50: [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};


// Here is what I tried but I didn't find any way to access a console.log that would return only a // single in the output.


const keysHello = Object.keys(nested);
console.log("keysHello", keysHello); // ['40', '50']

const values = Object.values(nested);
console.log("values", values); // [[{…}, {…}, {…}, {…}], [{…}, {…}, {…}, {…}])]

const keysValues = Object.entries(nested);
console.log("keysValues", keysValues); // [['40', [{…}, {…}, {…}, {…}]], ['50', [{…}, {…}, {…}, {…}]]

// The one below does not work
// const [, , {name}] = nested;
// console.log(`${Object.values[40]}`);

答案1

得分: 1

如果您知道要访问的键和索引,您可以像这样直接使用nested['40'][0].name

更通用的方式:

const nested = {
    40: [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    50: [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};

const key = '40';
const index = 0;

const { name } = nested[key][index];

请注意,您不能使用点符号访问nested.40,因为数值不能与点符号一起使用。

英文:

If you know the key and the index you need to access, you can just do nested['40'][0].name for instance.

More generic:

const nested = {
    40: [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    50: [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};

const key = '40';
const index = 0;

const { name } = nested[key][index];

See how you cannot do nested.40, as numeric values cannot be used with dot notation.

答案2

得分: 1

你似乎对你在这里操作的内容感到困惑。你首先有一个对象,所以const [, , {name}] = nested;不会起作用。这是一个对象,而不是一个数组。

对于Object.values[40]也是一样的情况。在这里,你有一个对象数组,所以在那个位置没有对象可以获取值。无论如何,你在这里使用Object.values的方式是不正确的。你应该这样做:Object.values(nested['40'][0])

尝试一下:

console.log(nested['40']);
console.log(nested['40'][0]);
console.log(Object.values(nested['40'][0]));

还值得一提的是,虽然你试图处理数字,但这些数字用于数组,而在这里使用对象键时,数字被强制转换(更改)为字符串。因此,最好直接使用字符串来避免混淆。

const nested = {
    '40': [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    '50': [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};
英文:

You seem to be getting confused on what you're working with here. You have an object first, so const [, , {name}] = nested; wouldn't do anything. It's an object, not an array.

Same for Object.values[40]. With that one you have an array of objects, so there's no object at that location to get values on. Anyway you're using Object.values incorrectly there. You would do something like Object.values(nested['40'][0]).

Try:

console.log(nested['40']);
console.log(nested['40'][0]);
console.log(Object.values(nested['40'][0]));

Also worth mentioning that although you are trying to work with numbers, those are used for arrays and numbers are coerced (changed) to strings here (when working with object keys). So it's best to just work with strings directly to avoid confusion.

const nested = {
    '40': [
        { hello: "1", name: "Max" },
        { hello: "2", name: "Julie" },
        { hello: "3", name: "Mark" },
        { hello: "4", name: "Isabella" },
    ],
    '50': [
        { hello: "1", name: "William" },
        { hello: "2", name: "James" },
        { hello: "3", name: "Lucas" },
        { hello: "4", name: "John" },
    ],
};

huangapple
  • 本文由 发表于 2023年4月17日 04:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定