使用 javascript 根据状态和商店计算对象?

count object based on status and shop using javascript?

我有以下 JSON 数组我想在每个对象中创建一个新字段,这将是对象的计数

我们必须根据状态、商店和名称(所有者详细信息)进行计数

我怎样才能做到这一点,我在下面添加了我的预期输出

 var items = [
  {
    "id": 1,    
    "status": "ORANGE",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]    
    
  },
  {
    "id": 2,
    "status": "GREEN",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]
  },
  {
    "id": 3,
    "status": "ORANGE",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]    
  },
  {
    "id": 4,
    "status": "YELLOW",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]
  },
  {
    "id": 5,
    "status": "RED",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]
  },
  {
    "id":6,
    "status": "GREEN",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]
  },
  {
    "id": 7,
    "status": "GREEN",
    "Shop":"XYZ",
    "ownerDetails":[ {"name":"test2","address":"test2"}]
  },
   {
    "id": 8,
    "status": "ORANGE",
    "Shop":"XYZ",
    "ownerDetails":[ {"name":"test2","address":"test2"}]    
  },
  {
    "id": 9,
    "status": "YELLOW",
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}]
  },
  {
    "id": 10,
    "status": "GREEN",
    "Shop":"EFG",
    "ownerDetails":[ {"name":"test3","address":"test3"}]
  }
] 

预期输出:所以根据每个商店、状态和名称(所有者详细信息)我们必须计算对象

    [
  {
    "id": 1,    
    "status": "ORANGE"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 2,
    "status": "GREEN"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 3,
    "status": "ORANGE"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 4,
    "status": "YELLOW"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 5,
    "status": "RED"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 1
  },
  {
    "id":6,
    "status": "GREEN"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 7,
    "status": "GREEN"
    "Shop":"XYZ",
    "ownerDetails":[ {"name":"test2","address":"test2"}],
    "Count": 1
  },
   {
    "id": 8,
    "status": "ORANGE"
    "Shop":"XYZ",
    "ownerDetails":[ {"name":"test2","address":"test2"}],
    "Count": 1
  },
  {
    "id": 9,
    "status": "YELLOW"
    "Shop":"ABC",
    "ownerDetails":[ {"name":"test1","address":"test1"}],
    "Count": 2
  },
  {
    "id": 10,
    "status": "GREEN"
    "Shop":"EFG"
    "ownerDetails":[ {"name":"test3","address":"test3"}],
    "Count": 1
  }
] 

有人可以帮我解决这个问题吗?

请看demo

感谢@Nico_ 的帮助

您可以使用 map 循环遍历您的数组(顺便说一句,您需要在“商店”之前添加一些昏迷。

const items = [[
  {
    "id": 1,    
    "status": "ORANGE",
    "Shop":"ABC"
  },
  {
    "id": 2,
    "status": "GREEN",
    "Shop":"ABC"
  },
]

const itemsWithCount = items.map(item => ({
  ...item,
  Count: items.filter(({ status, Shop }) => item.status === status && item.Shop === Shop).length
}));

对于数组中的每个项目,您保留该项目的当前值(...item)并添加一个 Count 属性,这将得到该项目的计数通过过滤数组以仅保留具有相同状态和商店的项目来获得该数组的长度。

var items = [{
    "id": 1,
    "status": "ORANGE",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]

  },
  {
    "id": 2,
    "status": "GREEN",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 3,
    "status": "ORANGE",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 4,
    "status": "YELLOW",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 5,
    "status": "RED",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 6,
    "status": "GREEN",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 7,
    "status": "GREEN",
    "Shop": "XYZ",
    "ownerDetails": [{
      "name": "test2",
      "address": "test2"
    }]
  },
  {
    "id": 8,
    "status": "ORANGE",
    "Shop": "XYZ",
    "ownerDetails": [{
      "name": "test2",
      "address": "test2"
    }]
  },
  {
    "id": 9,
    "status": "YELLOW",
    "Shop": "ABC",
    "ownerDetails": [{
      "name": "test1",
      "address": "test1"
    }]
  },
  {
    "id": 10,
    "status": "GREEN",
    "Shop": "EFG",
    "ownerDetails": [{
      "name": "test3",
      "address": "test3"
    }]
  }
];

var mapData = items.map((data) => {
  var getList = items.filter(word => word.Shop == data.Shop).length;
  return {
    id: data.id,
    status: data.status,
    Shop: data.Shop,
    text: data.ownerDetails,
    Count: getList
  };
});

console.log(mapData);

注意:- 地图数据和统计的类似商店....