如何操作特定对象? ,这是我的示例。

    "lead_tags": [{
        "tag_id": 8616,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 2147483647,
        "user_tags": null
    }, {
        "tag_id": 8656,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 2147483647,
        "user_tags": null
    }, {
        "tag_id": 8742,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 1249,
        "user_tags": {
            "user_tag_id": 1249,
            "user_id": 622,
            "tag_name": "Testtesttest"
        }   }, {
        "tag_id": 8767,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 1262,
        "user_tags": {
            "user_tag_id": 1262,
            "user_id": 622,
            "tag_name": "t"
        }   }]


首先,我需要过滤所有等于null的userTag

我使用过滤器不返回user_tags的值:null



var lead_tags = [{
        "tag_id": 8616,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 2147483647,
        "user_tags": null
    }, {
        "tag_id": 8656,
        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 2147483647,
        "user_tags": null
    }, {

        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 1249,
        "user_tags": {
            "user_tag_id": 1249,
            "user_id": 622,
            "tag_name": "Testtesttest",
            "tag_id": 8742
        }   }, {

        "user_id": 622,
        "tag_name": null,
        "tag_description": null,
        "user_tag_id": 1262,
        "user_tags": {
            "user_tag_id": 1262,
            "user_id": 622,
            "tag_name": "t",
            "tag_id": 8767
        }   }];



 var filter = _.filter(lead_tags, (data, index) => {
   return data.user_tags != null;
 });


console.log(filter);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>





现在我需要在开头获取tag_id并返回数据,如下所示

"lead_tags": [{

    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 1249,
    "user_tags": {
        "user_tag_id": 1249,
        "user_id": 622,
        "tag_name": "Testtesttest",
        "tag_id": 8742
    }
}, {

    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 1262,
    "user_tags": {
        "user_tag_id": 1262,
        "user_id": 622,
        "tag_name": "t",
        "tag_id": 8767
    }
}]


希望您能帮助我处理数据。

最佳答案

希望这项工作

var lead_tags = [{
    "tag_id": 8616,
    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 2147483647,
    "user_tags": null
}, {
    "tag_id": 8656,
    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 2147483647,
    "user_tags": null
}, {

    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 1249,
    "user_tags": {
        "user_tag_id": 1249,
        "user_id": 622,
        "tag_name": "Testtesttest",
        "tag_id": 8742
    }   }, {

    "user_id": 622,
    "tag_name": null,
    "tag_description": null,
    "user_tag_id": 1262,
    "user_tags": {
        "user_tag_id": 1262,
        "user_id": 622,
        "tag_name": "t",
        "tag_id": 8767
    }   }];



 var filter = lead_tags.filter(data => data.user_tags != null);
 let result = [];
   filter.forEach(el => {
      result = [
        ...result,
        {
          tag_id: el['user_tags']['tag_id'],
          user_id: el['user_id'],
          ...el,
         }
      ]
   });
   console.log(result);



const result2 = filter.reduce((acc, curr) => {
     return acc.concat({
          tag_id: curr['user_tags']['tag_id'],
          user_id: curr['user_id'],
          ...curr,
         })}, []);
   console.log(result2)

关于javascript - 操作对象数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60750236/

10-16 18:56