JS对象删除重复项

JS对象删除重复项

本文介绍了JS对象删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi ,
      
I need to remove duplicate entries.
ex : Here "Voltage" array is repeating, having one is enough.

Please help anyone to solve this.

      <pre lang="Javascript">var data =   [
               {
                  "metadata" : {
                        "names":["times","values","types"],
                        "types":["time","linear","ordinal"]
                  },
                  "data": [
                  ["0",2.37130,"Global"],
                  ["1",2.37130,"Voltage"],
                  ["1",2.37130,"Voltage"],
                  ["2",10.30980,"Intensity"],               

                  ],
               }
            ];</pre>





我尝试了什么:



我已经尝试过可能的控制台方法来查看所需的结果,但我找不到解决方案。



What I have tried:

I have tried possible console method to view the excpected result,but I could not find the solution.

推荐答案

 function removeDuplicates(array)
 {
     var targetArray = [];
     for (var i = 0; i < array.length; i++) {
         var current = array[i];
         var present = false;
         for (var j = 0; j < targetArray.length; j++) {
             var inner = targetArray[j];
             if (current[0] == inner[0] && current[1] == inner[1] && current[2] == inner[2])
             { present = true; break; }
         }
         if (!present)
             targetArray.push(current);
     }
     return targetArray;
 }



var data = [["0", 2.37130, "Global"],
            ["1", 2.37130, "Voltage"],
            ["1", 2.37130, "Voltage"],
            ["2", 10.30980, "Intensity"]];
 data = removeDuplicates(data);





演示: []


这篇关于JS对象删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 09:32