2016-04-18 7 views
-1

私はアイテムの配列を持っており、それがこのようなオブジェクトとして2つの項目があります。私は、アイテムの配列に新しい項目を追加する場合JavaScriptのオブジェクトと配列

var item = [{ 
     name: "audi", 
     body: "s1", 
     type: "cedan", 
     price: 10, 
     color: "red", 
     size: "large", 
     shape: "circle" 
    }, { 
     name: "bmw", 
     body: "x1", 
     type: "suv", 
     price: 20, 
     color: "blue", 
     size: "medium", 
     shape: "square" 
    }]; 

は、私はアイテムの配列から、次の配列を生成することができますどのように、私は意味私はそれがautomaticlay

var name = ["audi","bmw"]; 
var body = ["s1","x1"]; 
var color = ["red","blue"]; 
var size = ["large","medium"]; 
var shape = ["square","circle"]; 
var type = ["cedan","suv"]; 
var price = [10,20]; 
+0

'Array#map' ...? – Rayon

+0

キーと値のマップをそれぞれのキーの値の配列として探していますか? – gurvinder372

+0

はい、ただし、新しい配列の値を繰り返さずに –

答えて

3

あなたは

var names = output["name"]; 
を好きなように今、あなたは値の配列を取得することができ

var output = {}; 
item.forEach(function(val){ 
    Object.keys(val).forEach(function(key){ 
    output[key] = output[key] || []; 
    if(output[key].indexOf(val[key]) == -1) 
    { 
     output[key].push(val[key]); 
    } 
    }); 
}); 

を試し、その後、これらのキーのそれぞれの値の配列としてキーと値のマップを生成する場合

+0

いくつかの値のために同じ名前のアイテム配列に10アイテムがある場合、どのように私は反復を避けることができますか? –

+0

@ahmda配列にプッシュする前に、すでに存在するかどうかをチェックすることができます。更新された回答を確認します。 – gurvinder372

1

使用Array#mapmap()方法は、配列の全ての要素に与えられた関数を呼び出した結果、新しい配列を作成し、以下の新しいアレイに含めることにしたいです。

var item = [{ 
 
    name: "audi", 
 
    body: "s1", 
 
    type: "cedan", 
 
    price: 10, 
 
    color: "red", 
 
    size: "large", 
 
    shape: "circle" 
 
}, { 
 
    name: "bmw", 
 
    body: "x1", 
 
    type: "suv", 
 
    price: 20, 
 
    color: "blue", 
 
    size: "medium", 
 
    shape: "square" 
 
}]; 
 
var getArrByName = function(arr, key) { 
 
    return arr.map(function(item) { 
 
    return item[key]; 
 
    }) 
 
}; 
 

 
var name = getArrByName(item, 'name'); 
 
var body = getArrByName(item, 'body'); 
 
var color = getArrByName(item, 'color'); 
 
var size = getArrByName(item, 'size'); 
 
var shape = getArrByName(item, 'shape'); 
 
var type = getArrByName(item, 'type'); 
 
var price = getArrByName(item, 'price'); 
 
console.log(JSON.stringify(name)); 
 
console.log(JSON.stringify(body)); 
 
console.log(JSON.stringify(color)); 
 
console.log(JSON.stringify(size)); 
 
console.log(JSON.stringify(shape)); 
 
console.log(JSON.stringify(type)); 
 
console.log(JSON.stringify(price));
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

0
var names = item.map(function(el){return el.name}).filter(function(el,pos,arr){return arr.indexOf(el)=== pos}); 

重複することなく、あなたのすべての名前を取得します。必要な他の属性についても同じことができます。

+0

いくつかの値に対して同じ名前のアイテム配列に10個のアイテムがある場合、どのように反復を避けることができますか? –

+0

重複を削除するフィルタで更新された回答。私は1ライナーを好む –

1
//a utility 
var prop = key => obj => obj == null? void 0: obj[key]; 

var name = item.map(prop('name')); 
var body = item.map(prop('body')); 
//... 

または

var output = {}; 
Object.keys(item[0]).forEach(k => { 
    output[k] = item.map(prop(k)); 
}); 

または

var output = item.reduce((out, obj)=>{ 
    for(var k in obj) 
     (out[k] || (out[k] = [])).push(obj[k]); 
    return out; 
}, {}); 
0

車のリストと価格、名前のリストなどは、クラスなどの一般的なコードで囲む必要があります。

それはこのようなものになるだろう:それはあなたがすべての名前(など)または一意の名前のリストだけのリストをしたいかどうかは不明だ

class Cars { 

    constructor() { 
     this.carList = []; 
     this.nameList = []; 
     this.priceList = []; 
    } 

    addCar(newName, newPrice) { 
     this.carList.push({name: newName, price: newPrice}); 
     this.nameList.push(name); 
     this.priceList.push(price); 
    } 

    getCar(position) { 
     return this.carList[position]; 
    } 

    getNames() { 
     return this.nameList; 
    } 

    getPrices() { 
     return this.priceList; 
    } 
} 

var someCars = new Cars(); 

someCars.addCar("audi", 10); 
someCars.addCar("bmw", 20); 

var carNames = someCars.getNames(); 
var carPrices = someCars.getPrices(); 

。このコードはあなたにすべての名前を与えます。

関連する問題