按数组的嵌套对象过滤

时间:2018-01-31 04:08:16

标签: javascript ecmascript-6

我有以下对象数组

[  
   {  
      "uuid":"470ab23b-dab4-44ee-920f-36ea277a7c21",
      "distributeStoreUuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
      "substoreUuid":"9540c742-ba85-4086-bbc6-da0da9fbe31f",
      "substore__store_uuid":"e84ebdca-b676-4abd-9943-9a97abe8fcfe",
      "substore__ctrl_location_uuid":"619a55f1-789c-49a1-8870-14e3f66bdc1d",
      "substore__lt_status_uuid":"6d11644a-dd85-4201-a20a-4c5d186af601",
      "storedistribution":{  
         "uuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
         "acceptanceTaskDetailsUuid":"80fb9423-f979-4f53-841c-9f6af5f16ed3",
         "acceptanceTaskDetails__acceptanceTaskUuid":"acabb997-fc06-47ba-ae29-d7aea9a6a022",
         "acceptanceTask__lt_status_uuid":"15ceae6c-a8cb-416c-a6f3-ed7366740841",
      },
   }
]

使用以下解决方法

data.find(item => item.acceptanceTaskDetails__acceptanceTaskUuid === uuid)

但如果只使用find则不起作用。我也尝试filter并与reduce混淆。通过嵌套关键对象

获取对象过滤器数组的正确方法是什么

2 个答案:

答案 0 :(得分:1)

您可以使用filter获取具有相同v.uuidv.storedistribution.acceptanceTaskDetails__acceptanceTaskUuid

的元素



var arr = [  
	   {  
		  "uuid":"470ab23b-dab4-44ee-920f-36ea277a7c21",
		  "distributeStoreUuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
		  "substoreUuid":"9540c742-ba85-4086-bbc6-da0da9fbe31f",
		  "substore__store_uuid":"e84ebdca-b676-4abd-9943-9a97abe8fcfe",
		  "substore__ctrl_location_uuid":"619a55f1-789c-49a1-8870-14e3f66bdc1d",
		  "substore__lt_status_uuid":"6d11644a-dd85-4201-a20a-4c5d186af601",
		  "storedistribution":{  
			 "uuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
			 "acceptanceTaskDetailsUuid":"80fb9423-f979-4f53-841c-9f6af5f16ed3",
			 "acceptanceTaskDetails__acceptanceTaskUuid":"acabb997-fc06-47ba-ae29-d7aea9a6a022",
			 "acceptanceTask__lt_status_uuid":"15ceae6c-a8cb-416c-a6f3-ed7366740841",
		  },
	   },
	   {  
		  "uuid":"470ab23b-dab4-44ee-920f-36ea277a7c21",
		  "distributeStoreUuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
		  "substoreUuid":"9540c742-ba85-4086-bbc6-da0da9fbe31f",
		  "substore__store_uuid":"e84ebdca-b676-4abd-9943-9a97abe8fcfe",
		  "substore__ctrl_location_uuid":"619a55f1-789c-49a1-8870-14e3f66bdc1d",
		  "substore__lt_status_uuid":"6d11644a-dd85-4201-a20a-4c5d186af601",
		  "storedistribution":{  
			 "uuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
			 "acceptanceTaskDetailsUuid":"80fb9423-f979-4f53-841c-9f6af5f16ed3",
			 "acceptanceTaskDetails__acceptanceTaskUuid":"acabb997-fc06-47ba-ae29-d7aea9a6a022",
			 "acceptanceTask__lt_status_uuid":"15ceae6c-a8cb-416c-a6f3-ed7366740841",
		  },
	   },
	   {  
		  "uuid":"470ab23b-dab4-44ee-920f-fffffffffff",
		  "distributeStoreUuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
		  "substoreUuid":"9540c742-ba85-4086-bbc6-da0da9fbe31f",
		  "substore__store_uuid":"e84ebdca-b676-4abd-9943-9a97abe8fcfe",
		  "substore__ctrl_location_uuid":"619a55f1-789c-49a1-8870-14e3f66bdc1d",
		  "substore__lt_status_uuid":"6d11644a-dd85-4201-a20a-4c5d186af601",
		  "storedistribution":{  
			 "uuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
			 "acceptanceTaskDetailsUuid":"80fb9423-f979-4f53-841c-9f6af5f16ed3",
			 "acceptanceTaskDetails__acceptanceTaskUuid":"470ab23b-dab4-44ee-920f-fffffffffff",
			 "acceptanceTask__lt_status_uuid":"15ceae6c-a8cb-416c-a6f3-ed7366740841",
		  },
	   },
	];

	var result = arr.filter(v=>v.uuid === v.storedistribution.acceptanceTaskDetails__acceptanceTaskUuid);

	console.log( result );




答案 1 :(得分:0)

var array = [  
   {  
      "uuid":"470ab23b-dab4-44ee-920f-36ea277a7c21",
      "distributeStoreUuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
      "substoreUuid":"9540c742-ba85-4086-bbc6-da0da9fbe31f",
      "substore__store_uuid":"e84ebdca-b676-4abd-9943-9a97abe8fcfe",
      "substore__ctrl_location_uuid":"619a55f1-789c-49a1-8870-14e3f66bdc1d",
      "substore__lt_status_uuid":"6d11644a-dd85-4201-a20a-4c5d186af601",
      "storedistribution":{  
         "uuid":"af0309b5-5b84-46e5-b59f-5a35e84ffd92",
         "acceptanceTaskDetailsUuid":"80fb9423-f979-4f53-841c-9f6af5f16ed3",
         "acceptanceTaskDetails__acceptanceTaskUuid":"acabb997-fc06-47ba-ae29-d7aea9a6a022",
         "acceptanceTask__lt_status_uuid":"15ceae6c-a8cb-416c-a6f3-ed7366740841",
      }
   }
];

var result = array.filter(t=>t.storedistribution.acceptanceTaskDetails__acceptanceTaskUuid == 'acabb997-fc06-47ba-ae29-d7aea9a6a022');
console.log(result);