Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to create a JavasScript array in following format:
var datas = [
{ name: "Peter Pan", location: "peter#pan.de" },
{ name: "Molly", location: "molly#yahoo.com" },
{ name: "Forneria Marconi", location: "live#japan.jp" },
{ name: "Master <em>Sync</em>", location: "205bw#samsung.com" }
];
As I want to create it dynamically, It would be great if I can create an array like that.
You just did it.
But if you mean adding objects one at a time you can do
var datas = [];
and then
newobj = //dynamically created object
datas.push(newobj);
var data = [];
data.push({ name: "Peter Pan", location: "peter#pan.de" },
{ name: "Molly", location: "molly#yahoo.com" },
{ name: "Forneria Marconi", location: "live#japan.jp" },
{ name: "Master <em>Sync</em>", location: "205bw#samsung.com" });
If you are receiving the object from some service, just call data.push(objectName);. You will have array of objects.
Fiddle for you.
http://jsfiddle.net/q69ku/
Have a look at:
How do I create JavaScript array (JSON format) dynamically?
var data = [];
data.push({ name: "Peter Pan", location: "peter#pan.de" });
// ...
data.push({name: "x" location:"y"})
you will need to change this to fit your data results
var arr = [];
var b = 20; // or your data results
for(i=0;i<b;i++){
var obj = {name: "Peter Pan", location: "peter#pan.de"};
arr.push(obj);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have a dynamic array whose length keeps changing
Ex: let array = [1,2]
I have an array of objects, whose length also keeps changing:
let obj = [
{id: 1, value: abc, location: xyz},
{id: 2, value: pqr, location: xyz},
{id: 3, value: rqp, location: xyz}
]
I want to return a final array of objects which has the id of "array" in "obj" i.e
[
{id: 1, value: abc, location: xyz},
{id: 2, value: pqr, location: xyz}
]
Does filter work in this case?
You can use Array.prototype.filter and Array.prototype.includes for this.
obj.filter((item) => array.includes(item.id))
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have an array with objects possibly containing duplicate values. A cars array containing same brand and model but with different car types. I want each unique brand/model combination on a single row with an array per row containing the car types.
-- Received array structure
const carsInitialArray = [
{ brand: "Opel", model: "Astra", carType: "Sedan" },
{ brand: "Opel", model: "Astra", carType: "Hatchback" },
{ brand: "Opel", model: "Astra", carType: "Stationwagon" },
{ brand: "Opel", model: "Corsa", carType: "Hatchback" },
{ brand: "Opel", model: "Zafira", carType: "SUV" },
];
-- Wanted array structure
const carsFinalArray = [
{
brand: "Opel", model: "Astra", carTypes: ["Sedan", "Hatchback", "Stationwagon"],
},
{ brand: "Opel", model: "Corsa", carTypes: ["Hatchback"] },
{ brand: "Opel", model: "Zafira", carTypes: ["SUV"] },
];
I hope someone can help me out. Thanks in advance!
Try this, its a two step way, forEach rearranges the object into required format using key as assistant and Object.values converts it into required array.
const arr = {};
carsInitialArray.forEach(e=>{
if(arr[e.brand+e.model])
arr[e.brand+e.model].carTypes.push(e.carType);
else
arr[e.brand+e.model] ={brand:e.brand,model:e.model,carTypes:[e.carType]}
});
const required = Object.values(arr);
Here is a fiddle
https://jsfiddle.net/u2xm3tcn/1/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have database which is array of objects. It look like this:
export const BIKES: Bike[] = [
{
id: 1,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/10/Krypton_A18_2016_red_ultegra_16_1.jpg',
price: 28000,
discount: 71,
main: true,
shop: 'Canada Bike',
name: 'Argon 18',`
},
{
id: 2,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/03/etap.shadowpsd_2048x.png',
price: 7900,
discount: 41,
main: false,
shop: 'Amazon',
name: 'Aquila Cycles',
}
I want to subscribe to array of objects which will consist of 2 properties 'name' and 'shop' and theirs values.
It can be implemented using Array.map
const result = BIKES.map(({name, shop}) => ({ name, shop }));
console.log(result);
or
const result = BIKES.map((item) => {
return {
name: item.name,
shop: item.shop
};
});
console.log(result);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am doing a online course where I got the following assignment:
var peopleArray = [
{
name: "Harrison Ford",
occupation: "Actor"
},
{
name: "Justin Bieber",
occupation: "Singer"
},
{
name: "Vladimir Putin",
occupation: "Politician"
},
{
name: "Oprah",
occupation: "Entertainer"
}
]
Write a loop that pushes the names into a names array, and the occupations into an occupations array.
My question is: How do I write a for loop that pushes the names into a new array that is called Names, and occupations into a new array that is called Occupations? I am using Scrimba, so I will need to console log my results.
Looking forward to hearing from you!
var peopleArray = [
{
name: "Harrison Ford",
occupation: "Actor"
},
{
name: "Justin Bieber",
occupation: "Singer"
},
{
name: "Vladimir Putin",
occupation: "Politician"
},
{
name: "Oprah",
occupation: "Entertainer"
}
]
const names = []
const occupations = [];
peopleArray.forEach(el => {
names.push(el.name);
occupations.push(el.occupation);
});
console.warn(names, occupations);
https://jsfiddle.net/p1jz2qng/1/
I feel like you are going to assignments too fast. You should learn about arrays before doing that, since this assignment is pretty straightforward if you know anything about arrays.
// Original
var peopleArray = [
{
name: "Harrison Ford",
occupation: "Actor"
},
{
name: "Justin Bieber",
occupation: "Singer"
},
{
name: "Vladimir Putin",
occupation: "Politician"
},
{
name: "Oprah",
occupation: "Entertainer"
}
]
// Solution
let namesArray = []
let occupationsArray = []
for (let i = 0; i < peopleArray.length; i++){
namesArray.push(peopleArray[i].name)
occupationsArray.push(peopleArray[i].occupation)
}
console.log(namesArray)
console.log(occupationsArray)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am receiving the following data in the variable
let data = [
[{option: “foo0", value: “bar0”}],
[{option: “foo1", value: “bar1”}],
[{option: “foo2", value: “bar2”}],
[{option: “foo3", value: “bar3”}],
]
and I need to change the response to
[
{option: “foo0", value: “bar0”},
{option: “foo1", value: “bar1”},
{option: “foo2", value: “bar2”},
{option: “foo3", value: “bar3”},
]
how can i do it in JS
Use Array.flat():
const data = [[{"option":"foo0","value":"bar0"}],[{"option":"foo1","value":"bar1"}],[{"option":"foo2","value":"bar2"}],[{"option":"foo3","value":"bar3"}]]
const result = data.flat()
console.log(result)
If Array.flat() is not supported, you can flatten the array by spreading into concat:
const data = [[{"option":"foo0","value":"bar0"}],[{"option":"foo1","value":"bar1"}],[{"option":"foo2","value":"bar2"}],[{"option":"foo3","value":"bar3"}]]
const result = [].concat(...data)
console.log(result)
try this:
let data = [
[{
option: "foo0",
value: "bar0"
}],
[{
option: "foo1",
value: "bar1"
}],
[{
option: "foo2",
value: "bar2"
}],
[{
option: "foo3",
value: "bar3"
}],
];
let result = data.map(v => v[0]);
console.log(result);