2017-03-16 44 views
-1

我將某個對象的某個屬性的值從列表轉換爲鍵/值列表。Javascript將值轉換爲密鑰

我正在尋找不同的語法或更短的語法。

const lst = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }]; 
const obj = {}; 
lst.forEach((per) => { 
    obj[per.name] = true; 
}); 
console.log('obj', obj); 

結果:

{A: true, B: true} 
+0

heh。 「更先進」。有人應該包含一個使用with和gotos的例子。 –

+0

@KevinB你是什麼意思? – mehr

+1

您尚未指定「更高級」的含義。這是一個非常主觀的描述,可能對任何人都有意義。更好/更差的表現,更容易/更難以閱讀,更好/更差的效率等。 –

回答

0

您可以使用Object.assign()和傳播語法添加到對象,並在陣列可以map()到每個對象轉換爲{[e.name]: true}

const lst = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }]; 
 

 
const obj = Object.assign({}, ...lst.map(e => ({[e.name]: true}))) 
 
console.log(obj)

+2

這與SED unix語法一樣醜陋。我很傷心。我愛1995年以來的JS,這不再是JS :( – mplungjan

+1

@mplungjan但不過,但它「更先進」!這顯然使它成爲600%。 –

+0

哈哈:)是的。簡潔是偉大的。 Hello world:'++++++++++ [> +++++++> ++++++++++> +++ <<<-]> ++。> +。+++++ ++ .. +++。> ++。<< +++++++++++++++。>。+++。------ .------ - 。> +。'in [BrainFuck](https://en.wikipedia.org/wiki/Esoteric_programming_language#brainfuck) – mplungjan

0

你可能尋找array.reduce,對象擴散或它的API對口Object.assign,並計算屬性名稱。

var obj = lst.reduce((c, v) => ({ ...c, [v.name]: true }), {}); 

它本質上是如下:

var obj = lst.reduce(function(carry, value){ 
    var obj = {}; 
    obj[value.name] = true; 
    return Object.assign(carry, obj); 
}, {}); 
+1

你可能想在'()'中包裝'{... c,[v.name]:true}'! –

+0

約瑟夫你的代碼返回:未捕獲SyntaxError:意外的令牌... – mehr

+0

@mehr這是因爲我不指望你複製粘貼答案。對象傳播是新的語法,相當於我在回答中提到的Object.assign。 – Joseph

0

const lst = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }]; 
 
const obj = lst.reduce((a, o) => (a[o.name] = true, a), {}); 
 

 
console.log(obj);

上面是短的不言自明的:

const obj = lst.reduce(function(accumulator, current) { 
    return accumulator[current.name] = true, accumulator; // read about comma operator evaluation on MDN 
}, {}); 

和:

return accumulator[current.name] = true, accumulator; 

是一樣的:

accumulator[current.name] = true; 
return accumulator; 
+1

@Downvoter why? –

+0

這是「更先進」的方式? –

+0

它的功能更強大,因爲它在一個表達式中被評估! –

0

也許你可以在一個函數中使用它。我不是很確定,但它的作品適合我。

//In a fucntion 
    function asingListinObject(list, obj){ 
     //if obj comes undefined or null create one 
     if(obj == undefined || obj == null){ 
     obj = {}; 
     } 
     //do the asignment by the list 
     list.forEach((i_lts) => { 
     //validate if the 'name' exist 
     if(i_lts.name != undefined || i_lts.name != null){ 
      //New: set a diferent value 
      if(i_lts.value != undefined || i_lts.value != null){ 
      obj[i_lts.name] = i_lts.value; 
      }else{ 
      //Set a default value 
      obj[i_lts.name] = true; 
      } 
     } 
     }); 
     return obj; 
    } 

    const lst = [{ id: 1, name: 'A' }, { id: 2, name: 'B' }, 
    { id: 3, name: 'C', value: false }, { id: 4}]; 

    const obj_ref = {}; 
    const obj = asingListinObject(lst); 
    //works as references 
    asingListinObject(lst,obj_ref); 


    console.log('Obj_Ref', obj); 
    console.log('Obj', obj); 

結果,你都會有這樣的:因爲

Obj_Ref {A: true, B: true, C: false } 
    Obj {A: true, B: true, C: false } 

同值是相同的列表。我希望這個幫助。

相關問題