Манипуляции с массивом объекта - PullRequest
0 голосов
/ 19 марта 2020

Как я могу манипулировать указанным c объектом? Вот мой пример.

    "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

Я использую фильтр, чтобы не возвращать значение 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
    }
}]

Надеюсь, вы могли бы помочь мне в манипулировании данными.

Ответы [ 2 ]

2 голосов
/ 19 марта 2020

Надеюсь, что эта работа

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)
1 голос
/ 19 марта 2020

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


let userTagsArrObj = [];

for (let tagIndex = 0; tagIndex < lead_tags.length; tagIndex++) {
    const tagObject = lead_tags[tagIndex],
        { tag_id, user_tags } = tagObject;

    if (user_tags) {
      user_tags['tag_id'] = tag_id;
      userTagsArrObj.push(user_tags);
    }
}


console.log(userTagsArrObj);

Для дальнейшего использования

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

let result = lead_tags.filter((tagObject) => {
    if (tagObject.user_tags) {
        tagObject.user_tags['tag_id'] = tagObject.tag_id;
        return tagObject;
    }
});

console.log(result);

Подробнее Оптимизировать

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


let userTags = [];

for (let tagIndex = 0; tagIndex < lead_tags.length; tagIndex++) {
    const tagObject = lead_tags[tagIndex],
        tagId = tagObject.tag_id;

    let userTag = tagObject.user_tags;

    if (userTag) {
      userTag['tag_id'] = tagId;
      userTags.push(userTag);
    }
}

console.log(userTags);
...