简体   繁体   中英

Javascript creates new arrays based on conditions

I have old and entity arrays:

var old = [
    {
        "id": 3,
        "entity_type_id": 1,
        "product_id": 4,
        "name": "test1",
        "acreage": 100,
        "yield": 20,
        "worth": 30
    },
    {
        "id": 4,
        "entity_type_id": 1,
        "product_id": 4,
        "name": "test2",
        "acreage": 10,
        "yield": 20,
        "worth": 0
    },
    {
        "id": 5,
        "entity_type_id": 3,
        "product_id": 5,
        "name": "test3",
        "acreage": 20,
        "yield": 20,
        "worth": 40
    }
]
var entity = [
    {"id": 1, "name": "a1"},
    {"id": 2, "name": "a2"},
    {"id": 3, "name": "a3"}
]

I hope to get the following data:

 var newArr = [ { "id": 3, "entity_type_id": 1, "product_id": 4, "name": "test1", "acreage": 110, "yield": 40, "worth": 30, "entity_type_1": 2, // The total amount of entity_type_id (entity_type_id: 1) "entity_type_2": 0, "entity_type_3": 0 }, { "id": 5, "entity_type_id": 3, "product_id": 5, "name": "test3", "acreage": 20, "yield": 20, "worth": 40, "entity_type_1": 0, "entity_type_2": 0, "entity_type_3": 1 // The total amount of entity_type_id (entity_type_id: 3) } ] console.log(newArr)

I tried the following code and got some data. I'm not sure if there will be any exceptions or errors.

What's more, I don't know how to deal with the entity array data. Can someone help me solve this problem and get the result I expect?

Thank you very much !

function mergeArr(arr) {
    const temp = []
    arr.forEach((dataItem) => {
      if (temp.length) {
        let filterValue = temp.filter((items) => {
          return items.product_id === dataItem.product_id
        })
        if (filterValue.length) {
          temp.forEach((n) => {
            if (n.product_id === filterValue[0].product_id) {
              n.yield = dataItem.yield + filterValue[0].yield
              n.acreage = dataItem.acreage + filterValue[0].acreage
              n.worth = dataItem.worth + filterValue[0].worth
            }
          })
        } else {
          temp.push(dataItem)
        }
      } else {
        temp.push(dataItem)
      }
    })
    return temp
}

Youi could find the object and sum the wanted properties. For entity take another loop and map new entries and build a new object from it for spreading.

 var old = [{ id: 3, entity_type_id: 1, product_id: 4, name: "test1", acreage: 100, yield: 20, worth: 30 }, { id: 4, entity_type_id: 1, product_id: 4, name: "test2", acreage: 10, yield: 20, worth: 0 }, { id: 5, entity_type_id: 3, product_id: 5, name: "test3", acreage: 20, yield: 20, worth: 40 }], entity = [{ id: 1, name: "a1" }, { id: 2, name: "a2" }, { id: 3, name: "a3" }], entityTypes = Object.fromEntries(entity.map(({ id }) => ['entity_type_' + id, 0])), result = old.reduce((r, o) => { let temp = r.find(q => q.product_id === o.product_id); if (.temp) r.push(temp = {.., o. ..;entityTypes }), else ['acreage', 'yield'. 'worth'];forEach(k => temp[k] += o[k]). temp['entity_type_' + o;entity_type_id]++; return r, }; []). console;log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM