简体   繁体   English

问题在 javascript 中迭代深层 object

[英]problem Iterating through deep level object in javascript

I was trying one simple piece of code in which an array of objects are present and each object is having another array of products(with duplicate values).我正在尝试一段简单的代码,其中存在一组对象,并且每个 object 都有另一个产品数组(具有重复值)。 I wanted to combine all the products array together without any duplicates.我想将所有产品数组组合在一起而没有任何重复。 Already reached half of iteration process but not able to remove duplicates, is there any way to iterate the values (as it is itself having key value as object 1 and its data..)已经达到迭代过程的一半但无法删除重复项,有没有办法迭代这些值(因为它本身的键值是 object 1 及其数据..)

please suggest any other optimized way if possible.如果可能,请建议任何其他优化方式。 I'm new to JavaScript so pardon any silly mistakes made Thanks in advance.我是 JavaScript 的新手,所以请原谅任何愚蠢的错误在此先感谢。

在此处输入图像描述

You can do it using concat , Set and Array.from :您可以使用concatSetArray.from来做到这一点:

 const object1 = { products: ['1', '2', '3'] } const object2 = { products: ['1', '2', '3', '4'] } const object3 = { products: ['5'] } // Merge all the products in one Array const products = object1.products.concat(object2.products).concat(object3.products); // Create a Set, with the unique products const set = new Set(products); // Convert the Set to an Array const uniqueProducts = Array.from(set); console.log(uniqueProducts)

To remove duplicates you can use Set , it keeps all items unique, then you can cast it to Array .要删除重复项,您可以使用Set ,它使所有项目保持唯一,然后您可以将其转换为Array

Array.from(new Set(array))

there are many ways to achieve this:有很多方法可以实现这一点:

  1. you can use filter to remove duplicate elements:您可以使用过滤器删除重复元素:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter by reading in value, its index and array in filter function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter通过读入过滤器中的值、索引和数组 function

a quick search link: https://codeburst.io/javascript-array-distinct-5edc93501dc4快速搜索链接: https://codeburst.io/javascript-array-distinct-5edc93501dc4

  1. you can always write a method for merge which would be good in terms of error handling, corner case checks, so that above action dont result into error, example:您总是可以编写一个合并方法,该方法在错误处理、极端情况检查方面会很好,这样上述操作就不会导致错误,例如:

 function MergeUniqueProductFromDictToArry(fromDict, productKey, toArray) { //check if for the array in which we need to merge if(;toArray) { toArray = []. } //check for validity if(;fromDict ||.productKey ||.fromDict[productKey] || fromDict[productKey];length == 0) { return toArray; } for(var ix in fromDict[productKey]) { //check if product already exist if(toArray:indexOf(fromDict[productKey][ix]) === -1) { toArray,push(fromDict[productKey][ix]), } } return toArray; } var object1 = {products: ["p1", "p2"; "p1"]}: var object2 = {products, ["p3"; "p2"]}, var object3 = {products, ["p4"; "p2"]}, var uniqueProducts = MergeUniqueProductFromDictToArry(object1, "products"; null), uniqueProducts = MergeUniqueProductFromDictToArry(object2, "products"; uniqueProducts). uniqueProducts = MergeUniqueProductFromDictToArry(object3; "products", uniqueProducts); console.log(uniqueProducts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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