javascript किसी ऑब्जेक्ट के डेटा से दूसरे ऐरे में अलग एरेज़ बनाना




(8)

इसे इस्तेमाल करे

const list = [];
const names= data.map(d=>d.Name);
list.push(names);
const val = data.map(d=>d.accuracy);
list.push(val);

मान लीजिए कि मेरे पास यह खाली सरणी है:

var list = [];

यहाँ प्रश्न है: अगर मेरे पास data सरणी है जो इस तरह से 6 वस्तुओं से भरा है:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

मैं इस तरह data सरणी के डेटा के साथ list सरणी को कैसे आबाद कर सकता हूं:

var list = [
  ["Jack", "Sara", "Nick", "Doe", "Lee", "Sasha"],
  ["average", "good", "not bad", "good", "excellent", "good"]
];

मैंने बिना किसी भाग्य के लूप के लिए इसका उपयोग किया है:

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);


map ऑपरेटर का उपयोग करने का प्रयास करें:

list.push(data.map(n => n.Name), data.map(n => n.accuracy));
console.log(list);

जिन उप-सरणियों को आप पुश करना चाहते हैं, उन्हें शामिल करने के list घोषणा बदलें।

तब आपका कोड ठीक काम करता है

var list = [[],[]];


var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}
console.log(list);


यह एक त्रुटि फेंक देगा क्योंकि आपकी सूची सरणी खाली है और इसमें कोई सरणी नहीं है जो किसी मान को धक्का दे।

सूची सरणी आरम्भ करें जैसे:

var list = [
  [],
  []
];

पूरा कोड देखें:

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

var list = [
  [],
  []
];

for ( var i=0; i < data.length; i++ ) {
 list[0].push( data[i].Name );
 list[1].push( data[i].accuracy);
}

console.log(list);


data.reduce((acc, r) => ([[...acc[0], r.name], [...acc[1], r.accuracy]]), [[], []])

आप इसे नीचे की तरह लूप के साथ कर सकते हैं:

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

var list = [[],[]];

for (var i = 0; i < data.length; i++) {
  list[0].push(data[i].Name);
  list[1].push(data[i].accuracy);
}
console.log(list);

व्यक्तिगत रूप से मैं मानचित्र का उपयोग करूंगा:

var data = [];

data[0] = {
  Name: "Jack",
  accuracy: "average",
  accent: 79,
  weight: 1
};
data[1] = {
  Name: "Sara",
  accuracy: "good",
  accent: 24,
  weight: 2
};
data[2] = {
  Name: "Nick",
  accuracy: "not bad",
  accent: 89,
  weight: 1
};
data[3] = {
  Name: "Doe",
  accuracy: "good",
  accent: 88,
  weight: 4
};
data[4] = {
  Name: "Lee",
  accuracy: "excellent",
  accent: 63,
  weight: 9
};
data[5] = {
  Name: "Sasha",
  accuracy: "good",
  accent: 42,
  weight: 5
};

const names = data.map(item => item.Name)
const accuracies = data.map(item => item.accuracy)
const list = [names, accuracies]
console.log(list);


बस Array.prototype.map उपयोग Array.prototype.map जो एक मौजूदा से एक नया सरणी बनाने के लिए डिज़ाइन किया गया है, जहां परिणामी सरणी में मूल सरणी के समान लंबाई होगी:

const data = [{ Name: "Jack", accuracy: "average", accent: 79, weight: 1 },{ Name: "Sara", accuracy: "good", accent: 24, weight: 2 },{ Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 },{ Name: "Doe", accuracy: "good", accent: 88, weight: 4 },{ Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 },{ Name: "Sasha", accuracy: "good", accent: 42, weight: 5 }]

const list = [data.map(x=>x.Name), data.map(x=>x.accuracy)];

console.log(list);


यहाँ एक उदाहरण के साथ reduce :

var data = [];

data[0] = { Name: "Jack", accuracy: "average", accent: 79, weight: 1 };
data[1] = { Name: "Sara", accuracy: "good", accent: 24, weight: 2 };
data[2] = { Name: "Nick", accuracy: "not bad", accent: 89, weight: 1 };
data[3] = { Name: "Doe", accuracy: "good", accent: 88, weight: 4 };
data[4] = { Name: "Lee", accuracy: "excellent", accent: 63, weight: 9 };
data[5] = { Name: "Sasha", accuracy: "good", accent: 42, weight: 5 };

console.log(data.reduce((p,c) => p[0].push(c.Name) && p[1].push(c.accuracy) && p,[[],[]]))





javascript