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 3 years ago.
Improve this question
I want to rename the icon for every object in an array to uppercase and replace - by _.
Array:
[
{time: 1566255600, summary: "Delno oblačno čez dan.", icon: "partly-cloudy-day", sunriseTime: 1566276904, sunsetTime: 1566328534, …},
{time: 1566342000, summary: "Pretežno oblačno čez dan.", icon: "partly-cloudy-day", sunriseTime: 1566363400, sunsetTime: 1566414810, …},
{time: 1566428400, summary: "Pretežno oblačno čez dan.", icon: "partly-cloudy-day", sunriseTime: 1566449896, sunsetTime: 1566501085, …},
{time: 1566514800, summary: "Pretežno oblačno čez dan.", icon: "partly-cloudy-day", ....}
]
const daysForecast = forecast.daily.data; //api call that reterun array with object
var daysForecastNewIcon = [];
daysForecast.forEach(function(item){
item.icon.replace(/-/g,"_").toUpperCase();
daysForecastNewIcon.push(item)
});
Try this
const daysForecast = forecast.daily.data.map(obj => { obj.icon = obj.icon.replace(/-/, "_").toUpperCase(); return 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 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 1 year ago.
Improve this question
I have an array like this:
0: {id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red"}
And i need to modify it to have like that:
{id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red"}
If you need to unwrap the first value, you can destructure it fairly easy. Make sure the variable is a let or var and not a const.
let data = [{ id: "GD721OGWRF", quantity: "1", Size: "Medium", Colour: "Red" }];
[data] = data; // Destructure the first item (unwrap the first result)
console.log(data); // Print the modified object.
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 3 years ago.
Improve this question
I have an array like this
let data = ['String1', 'String2', 'String3', 'String4']
I need to convert this array to an array of objects like so:
data = [
{value: 0, label: 'String1'},
{value: 1, label: 'String2'},
{value: 2, label: 'String3'},
{value: 3, label: 'String4'}
]
How to achieve this most elegantly?
Use map():
const arr = ['String1', 'String2', 'String3', 'String4'];
const res = arr.map((label, value) => ({ value, label }));
console.log(res);
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);
}