我有一个看起来像这样的对象:
var obj = {
"array1": [
{
"label": "something1",
"ref": "option2a"
},
{
"label": "something2",
"ref": "option2b"
},
{
"label": "something3",
"ref": "option2a"
},
{
"label": "something4",
"ref": "option2a"
},
{
"label": "something5",
"ref": "option2a"
}
],
"array2": [
{
"label": "something6 is the longest label",
"ref": "option3a"
},
{
"label": "Other",
"ref": "option3b"
}
]
}
我想获取最长标签的长度。在这种情况下,我想返回17。因为最长的标签是“ somethinglongest”,其长度为17。
答案 0 :(得分:4)
.concat()
和Object.values()
获得包含所有对象的单个数组。.map()
。.reduce()
来获取最长字符串的长度。
let data = {
"array1": [
{"label": "something1", "ref": "option2a"},
{"label": "something2", "ref": "option2b"},
{"label": "something3", "ref": "option2a"},
{"label": "something4", "ref": "option2a"},
{"label": "something5", "ref": "option2a"}
],
"array2": [
{"label": "somethinglongest", "ref": "option3a"},
{"label": "Other", "ref": "option3b"}
]
};
let reducer = arr => [].concat(...Object.values(arr))
.map(({ label }) => label.length)
.reduce((r, c) => r > c ? r : c);
console.log(reducer(data));
答案 1 :(得分:4)
获取子数组中所有对象的数组。
计算将每个值映射到其长度的值的最大长度:
var obj = {
"array1": [{
"label": "something1",
"ref": "option2a"
},
{
"label": "something2",
"ref": "option2b"
},
{
"label": "something3",
"ref": "option2a"
},
{
"label": "something4",
"ref": "option2a"
},
{
"label": "something5",
"ref": "option2a"
}
],
"array2": [{
"label": "somethinglongest",
"ref": "option3a"
},
{
"label": "Other",
"ref": "option3b"
}
]
};
console.log(Math.max(...Object.values(obj).flat().map(o => o.label.length)));
答案 2 :(得分:2)
直接的方法:
let obj = {
"array1": [
{"label": "something1", "ref": "option2a"},
{"label": "something2", "ref": "option2b"},
{"label": "something3", "ref": "option2a"},
{"label": "something4", "ref": "option2a"},
{"label": "something5", "ref": "option2a"}
],
"array2": [
{"label": "something6 is the longest label", "ref": "option3a"},
{"label": "Other", "ref": "option3b"}
]
};
let longest = 0
for (key in obj){
if (!obj.hasOwnProperty(key)) continue;
for (let x of obj[key]){
if (x.label.length > longest)
longest = x.label.length;
}
}
console.log(longest);
答案 3 :(得分:1)
另一种选择是遍历obj
对象自己的属性,并检查每个标签的长度是否大于以前的最长长度。
var obj = {
"array1": [
{ "label": "something1", "ref": "option2a" },
{ "label": "something2", "ref": "option2b" },
{ "label": "something3", "ref": "option2a" },
{ "label": "something4", "ref": "option2a" },
{ "label": "something5", "ref": "option2a" }
], "array2": [
{ "label": "somethinglongest", "ref": "option3a" },
{ "label": "Other", "ref": "option3b" }
]
}
let longest = 0;
for (key in obj) {
if(obj.hasOwnProperty(key)) {
obj[key].forEach(item => {
if (item.label.length > longest) longest = item.label.length;
})
}
}
console.log(longest);
答案 4 :(得分:1)
您可以先寻找对象,然后寻找想要的钥匙。这适用于任意深度的数据。
function getLongest(object, key) {
return Object.values(object).reduce((l, v) => {
if (key in v) return Math.max(l, v[key].length);
if (v && typeof v === 'object') return Math.max(l, getLongest(v, key));
return l;
}, 0);
}
var data = { array1: [{ label: "something1", ref: "option2a" }, { label: "something2", ref: "option2b" }, { label: "something3", ref: "option2a" }, { label: "something4", ref: "option2a" }, { label: "something5", ref: "option2a" }], array2: [{ label: "somethinglongest", ref: "option3a" }, { label: "Other", ref: "option3b" }] };
console.log(getLongest(data, 'label'));
答案 5 :(得分:0)
您可以使用此1行语句。
let maxLen = Math.max(...Object.keys(obj).map((key) => Math.max(...obj[key].map((o) => o.label.length))));
关于节点REPL的示例
> var obj = {
... "array1": [
... {
..... "label": "something1",
..... "ref": "option2a"
..... },
... {
..... "label": "something2",
..... "ref": "option2b"
..... },
... {
..... "label": "something3",
..... "ref": "option2a"
..... },
... {
..... "label": "something4",
..... "ref": "option2a"
..... },
... {
..... "label": "something5",
..... "ref": "option2a"
..... }
... ],
... "array2": [
... {
..... "label": "somethinglongest",
..... "ref": "option3a"
..... },
... {
..... "label": "Other",
..... "ref": "option3b"
..... }
... ]
... }
undefined
>
> Math.max(...Object.keys(obj).map((key) => Math.max(...obj[key].map((o) => o.label.length))))
16
>
详细概述
let keys = Object.keys(obj);
let maxLens = keys.map((key) => {
let arr = obj[key];
return Math.max(...arr.map((o) => o.label.length))
});
let max = Math.max(...maxLens);
console.log(keys); // [ 'array1', 'array2' ]
console.log(maxLens); // [ 10, 16 ]
console.log(max); // 16
这是帮助您的示例。
> Math.max(4, 5, 7)
7
>
> Math.max([4, 5, 7])
NaN
>
> Math.max(...[6, 7])
7
>
> let a = [2, 4, 5]
undefined
>
> a2 = a.map((num) => num ** 2)
[ 4, 16, 25 ]
>
> a3 = a.map((num) => num + 2)
[ 4, 6, 7 ]
>