【问题标题】:How to get a list of IDS from first and second level array如何从一级和二级数组中获取 IDS 列表
【发布时间】:2020-07-03 10:12:49
【问题描述】:

我在 JSON/JavaScript 中有以下结构:

{
  "comments": [
    {
      "id": 1,
      "content": "lorem ipsum",
      "answers": []
    },
    {
      "id": 2,
      "content": "lorem ipsum",
      "answers": [
        {
          "id": 30,
          "content": "lorem ipsum"
        }
      ]
    },
    {
      "id": 3,
      "content": "lorem ipsum",
      "answers": [
        {
          "id": 99,
          "content": "lorem ipsum"
        },
        {
          "id": 103,
          "content": "lorem ipsum"
        }
      ]
    },
    {
      "id": 5,
      "content": "comment",
      "answers": []
    }
  ]
}

我需要创建一个包含所有 cmets 的 id 的数组,包括答案类型 cmets

结尾应该是这样的

[1, 2, 3, 5, 30, 99, 103]

我可以只使用mapreducefilter 吗?

接下来我可以尝试什么?

【问题讨论】:

  • 对于对象中的所有 id 字段:let ids = JSON.stringify(obj.comments).match(/\"?id\"?\:(\d+)\,/g).map((id)=>{return parseInt(id.replace(/\"?id\"?\:(\d+)\,/,'$1'));});。不准确的解决方案,但按预期提供输出。

标签: javascript json typescript


【解决方案1】:

如果你也可以使用flat:

const data = {comments:[{id:1,content:"lorem ipsum",answers:[]},{id:2,content:"lorem ipsum",answers:[{id:30,content:"lorem ipsum"}]},{id:3,content:"lorem ipsum",answers:[{id:99,content:"lorem ipsum"},{id:103,content:"lorem ipsum"}]},{id:5,content:"comment",answers:[]}]};

function findIds(obj) {
  return obj.comments.map(com => {
    return [ com.id, ...com.answers.map(ans => ans.id) ]
  })
  .flat()
  .sort((a, b) => a - b);
}

console.log( findIds(data) ); // [1,2,3,5,30,99,103]

否则,只使用mapreduce

const data = {comments:[{id:1,content:"lorem ipsum",answers:[]},{id:2,content:"lorem ipsum",answers:[{id:30,content:"lorem ipsum"}]},{id:3,content:"lorem ipsum",answers:[{id:99,content:"lorem ipsum"},{id:103,content:"lorem ipsum"}]},{id:5,content:"comment",answers:[]}]};

function findIds(obj) {
  return obj.comments.reduce((res, com) => {
    return [ ...res, com.id, ...com.answers.map(ans => ans.id) ]
  }, [])
  .sort((a, b) => a - b);
}

console.log( findIds(data) ); // [1,2,3,5,30,99,103]

【讨论】:

  • 好的,我添加了排序
  • 什么是...res
  • @YungSilva 对不起,既然你提到了reduce,我以为你知道如何使用它。这是正在构建的result。它在these docs 中被称为“累加器”。或者也许你在谈论...?这是Spread syntax。基本上相当于做res.concat(otherArray)(合并数组)
【解决方案2】:

您可以使用数组的Spread_syntax/concatreducemapsort 方法实现所需的输出。

请看下面的代码 sn-p:

ES6

var obj = {"comments":[{"id":1,"content":"lorem ipsum","answers":[]},{"id":2,"content":"lorem ipsum","answers":[{"id":30,"content":"lorem ipsum"}]},{"id":3,"content":"lorem ipsum","answers":[{"id":99,"content":"lorem ipsum"},{"id":103,"content":"lorem ipsum"}]},{"id":5,"content":"comment","answers":[]}]};

let result = obj.comments.reduce((r,o)=>[...r, o.id,...o.answers.map(v=>v.id)],[]);

console.log(result.sort((a,b)=> a-b));

ES5

var data = {"comments":[{"id":1,"content":"lorem ipsum","answers":[]},{"id":2,"content":"lorem ipsum","answers":[{"id":30,"content":"lorem ipsum"}]},{"id":3,"content":"lorem ipsum","answers":[{"id":99,"content":"lorem ipsum"},{"id":103,"content":"lorem ipsum"}]},{"id":5,"content":"comment","answers":[]}]};

//This function will return the all id's from the passed array as per above object data.
function getIDs(obj) {
  var result = obj.comments.reduce(function(res, item) {
    var answers = item.answers.map(function(ans) {
      return ans.id;
    });
    res.push(item.id);//Adding id in array from main list
    return res.concat(answers);//Using contact method for add both arrays values 
  }, []);

  return result.sort(function(a, b) {
    return a - b;
  });
}


console.log(getIDs(data));

【讨论】:

  • 什么是...r
  • @YungSilva ...r is Spread syntax (...) 允许在零个或多个参数(用于函数调用)或元素(用于数组文字)的地方扩展数组表达式或字符串等可迭代对象预期,或者在预期零个或多个键值对(对于对象文字)的地方扩展对象表达式。
【解决方案3】:

试试这个代码

你的 JSON:data__ = { ... }

let result = []
let innerObjs = []
for (let i = 0; i < data__.comments.length; i++){
    let obj = data__.comments[i].id
    result.push(obj)
    innerObjs.push(data__.comments[i].answers)
}
for(let a = 0; a < innerObjs.length; a ++){
    let answers = innerObjs[a]
    for(let j =0; j < answers.length; j++){
        let ans = answers[j]
        if(ans)             
            result.push(ans.id)
    }
}
console.log(result)

【讨论】:

    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多