【问题标题】:How find properties in an array in object and move these objects to another array in the object?如何在对象的数组中查找属性并将这些对象移动到对象中的另一个数组?
【发布时间】:2019-06-13 00:26:52
【问题描述】:

如何在 object 的数组中找到属性并将这些对象移动到 object 中的另一个数组中?如何从this.state.comments 中提取{"comment": "value"} 并将其放在comments 数组中的comment 对象中,位于其他对象之后? 我重新映射了数组并自己提取了值 - comment properties。如何提取整个对象,使其看起来像这样 {"comment:" value "}

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
    }

this.state.comments = [
    {"comment": "eeeee"},
    {"comment": "rrrrr"},
    {"comment": "ggggg"},
    {"date: Thu Jun 13 2019 01:27:09 
      "desc": "dfdfdf"
      "comment": "hhhhhh"
    }
]

let v = this.state.comments.map(t => t.comment? t.comment : t);

 console.log(`{comment: ${v}`);

预期效果:

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}, 
           {"comment": "eeeee"}, {"comment": "rrrrr"}, {"comment": 
           "ggggg"}, "comment": "hhhhhh"]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
}

【问题讨论】:

    标签: javascript arrays reactjs ecmascript-6 javascript-objects


    【解决方案1】:

    const comment = {
            "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}],
            "description": " ytytyty",
            "id": 3,
            "title": "gfgfgfgfgf"
        }
    
    let newComments = [
        {"comment": "eeeee"},
        {"comment": "rrrrr"},
        {"comment": "ggggg"},
        {"date": "Thu Jun 13 2019 01:27:09", 
          "desc": "dfdfdf",
          "comment": "hhhhhh"
        }
    ]
    
    newComments.forEach(t => {
      if( t.comment ) comment.comments.push({
        comment: t.comment
      })
    });
    
     console.log(comment);

    【讨论】:

    【解决方案2】:

    const comment = {
            comments: [{comment: 'aaa'}, {comment: 'bbb'}],
            description: " ytytyty",
            id: 3,
            title: "gfgfgfgfgf"
        }
    
    const newComments = [
        {comment: "eeeee"},
        {comment: "rrrrr"},
        {comment: "ggggg"},
        {date: "Thu Jun 13 2019 01:27:09", 
          desc: "dfdfdf",
          comment: "hhhhhh"
        }
    ];
    
    comment.comments = newComments.reduce((res,obj) => obj.comment ? [...res, {comment : obj.comment}] : res,comment.comments || [])
    
    
    
     console.log(comment);

    【讨论】:

    • 我想在react中使用它,但是时间翻了一番。 ://stackblitz.com/edit/react-jfkwnu
    【解决方案3】:

    只需使用forEach 遍历每个项目并检查键是否为comment - 如果是,则推送到comments 数组。

    const comment = {"comments":[{'comment':'aaa'},{'comment':'bbb'}], "description":" ytytyty", "id":3, "title":"gfgfgfgfgf"};
    const state = {comments:[{"comment":"eeeee"},{"comment":"rrrrr"},{"comment":"ggggg"},{"date":"Thu Jun 13 2019 01:27:09", "desc":"dfdfdf", "comment":"hhhhhh"}]};
    state.comments.forEach(({ comment: c }) => c ? comment.comments.push({ c }) : c);
    console.log(comment);
    .as-console-wrapper { max-height: 100% !important; top: auto; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2019-05-02
      • 2011-08-29
      • 2016-04-20
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多