Object of array of objects. Get the length of all values with the same property

问题: I have an object that looks like this: var obj = { "array1": [ { "label": "something1", "ref": "option2a" }, {...

问题:

I have an object that looks like this:

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"
        }
      ]
    }

I want to get the length of the longest label. In this case I would want to return 17. Because the longest label is "somethinglongest" which has a length of 17.


回答1:

A straight-forward approach:

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);


回答2:

  • Use .concat() and Object.values() to get a single array having all objects.
  • Use .map() to an array containing length of strings.
  • And finally use .reduce() to get the length of longest string.

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));


回答3:

Get an array of all the objects inside the sub arrays.

Calculate the max length of the values mapping each value to its length:

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)));


回答4:

Another option is to iterate through the own properties of the obj object and check if the length of each label is longer than the previous longest length.

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);


回答5:

You could look for objects and then for the wanted key. This works for an arbitrary depth of data.

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'));


回答6:

You can use this 1 line statement.

let maxLen = Math.max(...Object.keys(obj).map((key) => Math.max(...obj[key].map((o) => o.label.length))));

Example on Node 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
>

Detailed overview

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

And here is the example to help you.

> 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 ]
>
  • 发表于 2019-01-12 02:18
  • 阅读 ( 186 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除