2016-11-10 36 views
0

我有一個對象的數組,我需要轉換爲表(一種數據透視)。說到這一點,我需要獲得另一個對象數組,其中包含具有嵌套對象數組的唯一標題以及值對。你可以看看這個,並幫助解決我需要的東西嗎?請參閱原件及以下所需的陣列:如何在JavaScript中旋轉對象數組?

原始數組:

[ 
{title: "Title1", value1: "value1", value2: "value2"}, 
{title: "Title2", value1: "value1", value2: "value2"}, 
{title: "Title1", value1: "value1", value2: "value2"}, 
{title: "Title3", value1: "value1", value2: "value2"}, 
{title: "Title2", value1: "value1", value2: "value2"}, 
{title: "Title1", value1: "value1", value2: "value2"}, 
{title: "Title3", value1: "value1", value2: "value2"}, 
{title: "Title1", value1: "value1", value2: "value2"}, 
] 

期望的結果:

[ 
{title: "Title1", values: [{value1: "value1"}, {value2: "value2"}]}, 
{title: "Title2", values: [{value1: "value1"}, {value2: "value2"}]}, 
{title: "Title3", values: [{value1: "value1"}, {value2: "value2"}]}, 
] 

感謝,如果提前的任何建議。

+2

對於您的示例來說,太多重複的「value1」「value2」也無濟於事。 – Paulpro

+0

結果真的是想要的結果? –

回答

1

您可以使用散列表進行分組,如thisArgArray#forEach

var data = [{ title: "Title1", value1: "value1", value2: "value2" }, { title: "Title2", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }, { title: "Title3", value1: "value1", value2: "value2" }, { title: "Title2", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }, { title: "Title3", value1: "value1", value2: "value2" }, { title: "Title1", value1: "value1", value2: "value2" }], 
 
    grouped = []; 
 

 
data.forEach(function (a) { 
 

 
    // check if title is not in hash table 
 
    if (!this[a.title]) { 
 

 
     // if not, create new object with title and values array 
 
     // and assign it with the title as hash to the hash table 
 
     this[a.title] = { title: a.title, values: [] }; 
 

 
     // add the new object to the result set, too 
 
     grouped.push(this[a.title]); 
 
    } 
 

 
    // create a new object with the other values and push it 
 
    // to the array of the object of the hash table 
 
    this[a.title].values.push({ value1: a.value1, value2: a.value2 }); 
 
}, Object.create(null)); // Object.create creates an empty object without prototypes 
 

 
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

謝謝,妮娜!這正是我想要的。請允許我向代碼添加一些註釋,因爲我希望更好地理解我們在這裏的內容。然而,這並不重要,如果你沒有時間,我會試着自己調查一切。 –

1
var originalArray = [ 
     {title: "Title1", value1: "value1", value2: "value2"}, 
     {title: "Title2", value1: "value1", value2: "value2"}, 
     {title: "Title1", value1: "value1", value2: "value2"}, 
     {title: "Title3", value1: "value1", value2: "value2"}, 
     {title: "Title2", value1: "value1", value2: "value2"}, 
     {title: "Title1", value1: "value1", value2: "value2"}, 
     {title: "Title3", value1: "value1", value2: "value2"}, 
     {title: "Title1", value1: "value1", value2: "value2"}, 
    ]; 
    var newArray = []; 

    for(var i = 0; i<originalArray.length; i++){ 
     var object = {}; 
     object.title=originalArray[i].title; 
     var values = []; 
     values.push({value1: originalArray[i].value1}); 
     values.push({value2: originalArray[i].value2}); 
     object.values= values; 
     newArray.push(object); 
    } 

這將工作,但說實話,我不知道爲什麼你有一些重複的地契。我認爲這只是一個例子。如果不是,答案是另一回答。

1

一個簡單的陣列減少可能會有所幫助

Reduce

Find

使用

let data = [{ 
 
    title: "Title1", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title2", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title1", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title3", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title2", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title1", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title3", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}, { 
 
    title: "Title1", 
 
    value1: "value1", 
 
    value2: "value2" 
 
}]; 
 

 
let pivoted = data.reduce((prev, cur) => { 
 
    let existing = prev.find(x => x.title === cur.title); 
 

 
    if (existing) 
 
    existing.values.push(cur) 
 
    else 
 
    prev.push({ 
 
     title: cur.title, 
 
     values: [cur] 
 
    }); 
 

 
    return prev; 
 
}, []); 
 

 
console.log(pivoted);

API的

這然後可以通過你的願望,那就需要的錯誤處理增加,如果該字段不但不存在任何領域適應支點:

let pivotBy = (key, data) => data.reduce((prev, cur) => { 
    let existing = prev.find(x => x[key] === cur[key]); 

    if (existing) { 
    existing.values.push(cur); 
    } else { 
    let toInsert = { 
     values: [cur] 
    } 

    toInsert[key] = cur[key]; 

    prev.push(toInsert); 
    } 

    return prev; 
}, []); 

編輯 我誤解你所需的輸出格式,但是減少方法可以修改以滿足這一要求。

相關問題