2016-07-02 88 views
1

我找不到類似的問題,我有點卡住了。我有以下的JSON數組:從對象數組中返回一個數組的唯一數組值

[ 
    { 
     "Name": "element1", 
     "Attributes": ["1", "2"] 
    }, 

    { 
     "Name": "element2", 
     "Attributes": ["1","3" ] 
    }, 
    { 
     "Name": "element3", 
     "Attributes": [] 
    } 
] 

我試圖在「屬性」屬性創建所有的獨特元素的數組,但我無法通過每個對象循環,然後通過循環數組元素返回唯一值。我試圖用filter()或map()來完成。

編輯:我想要一個獨特的元素數組,所以:[1,2,3]。

+0

請添加想要的結果,你嘗試過的代碼。 –

+0

I'vr添加了我想要的輸出,但我堅持如何在map()中執行過濾器(),並返回內部過濾器()的輸出以備份包含映射() – DorianHuxley

+0

可能的重複[訪問/進程(嵌套)對象,數組或JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu

回答

1

您可以用幾個Array方法做到這一點。例如:

var result = [ 
 
    { 
 
     "Name": "element1", 
 
     "Attributes": ["1", "2"] 
 
    }, 
 

 
    { 
 
     "Name": "element2", 
 
     "Attributes": ["1","3" ] 
 
    }, 
 
    { 
 
     "Name": "element3", 
 
     "Attributes": [] 
 
    } 
 
] 
 

 
// map to [ ["1", "2"], ["1", "3"], [] ] 
 
.map(item => item.Attributes) 
 

 
// flatten to [ "1", "2", "1", "3" ] 
 
.reduce((prev, curr) => prev.concat(curr), []) 
 

 
// filter unique [ "1", "2", "3" ] 
 
.filter((item, i, arr) => arr.indexOf(item) === i) 
 

 
console.log(result)

+0

沒有必要使用'apply','return prev.concat(curr)'就足夠了 –

0
var uniqueArr = []; 

var arr = [ 
    { 
     "Name": "element1", 
     "Attributes": ["1", "2"] 
    }, 

    { 
     "Name": "element2", 
     "Attributes": ["1","3" ] 
    }, 
    { 
     "Name": "element3", 
     "Attributes": [] 
    } 
]; 

arr.forEach(function(obj) { 
    var attr = obj.Attributes; 
    attr.forEach(function(val){ 
     if (uniqueArray.indexOf(val) < 0) { 
      uniqueArray.push(val) 
     } 
    }); 
}) 
1

您可以使用Array#reduceArray#filter方法

var data = [{ 
 
    "Name": "element1", 
 
    "Attributes": ["1", "2"] 
 
    }, 
 

 
    { 
 
    "Name": "element2", 
 
    "Attributes": ["1", "3"] 
 
    }, { 
 
    "Name": "element3", 
 
    "Attributes": [] 
 
    } 
 
] 
 

 
console.log(
 
    // iterate over array elements 
 
    data.reduce(function(arr, ele) { 
 
    // push the unique values to array 
 
    [].push.apply(arr, 
 
     // filter out unique value 
 
     ele.Attributes.filter(function(v) { 
 
     // check element present in array 
 
     return arr.indexOf(v) == -1; 
 
     }) 
 
    ); 
 
    // return the unique array 
 
    return arr; 
 
    // set initial argument as an empty array 
 
    }, []) 
 
);


隨着ES6 arrow function

var data = [{ 
 
    "Name": "element1", 
 
    "Attributes": ["1", "2"] 
 
    }, 
 

 
    { 
 
    "Name": "element2", 
 
    "Attributes": ["1", "3"] 
 
    }, { 
 
    "Name": "element3", 
 
    "Attributes": [] 
 
    } 
 
] 
 

 
console.log(
 
    data.reduce((arr, ele) => ([].push.apply(arr, ele.Attributes.filter((v) => arr.indexOf(v) == -1)), arr), []) 
 
);

0

如果lodash是一個選項,你可以很容易地得到你想要的東西:

> _.chain(foo).map('Attributes').flatten().uniq().value() 
["1", "2", "3"] 
0

您有答案可供選擇。只是爲了好玩:此人使用ES6

"use strict"; 
 
let uniqueAttr = []; 
 
const obj = [ 
 
    { 
 
     "Name": "element1", 
 
     "Attributes": ["1", "2"] 
 
    }, 
 

 
    { 
 
     "Name": "element2", 
 
     "Attributes": ["1","3" ] 
 
    }, 
 
    { 
 
     "Name": "element3", 
 
     "Attributes": [] 
 
    } 
 
]; 
 

 
obj.forEach(element => 
 
    element.Attributes.forEach( 
 
    attr => uniqueAttr.indexOf(attr) < 0 && uniqueAttr.push(attr) 
 
) 
 
); 
 

 
document.querySelector("#result").textContent = uniqueAttr;
<pre id="result"></pre>

0

試試這個,這將有助於解決這一問題。

var data = [{ 
 
    "Name": "element1", 
 
    "Attributes": ["1", "2"] 
 
    }, { 
 
    "Name": "element2", 
 
    "Attributes": ["1", "3"] 
 
    }, { 
 
    "Name": "element3", 
 
    "Attributes": [] 
 
    }]; 
 
    var Attributes = []; 
 
    $.each(data, function(i, e) { 
 
    $.each(e.Attributes, function(i, e) { 
 
     Attributes.push(parseInt(e)); 
 
    }); 
 
    }); 
 
    Attributes = $.unique(Attributes); 
 
    alert(Attributes);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

相關問題