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.
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 last month.
Improve this question
I have an array of words in a .json file
Like this:
[
"city",
"river",
"car"
]
And i want to get an object for each word
Something like this:
[
{
word: "city",
something: "..."
},
{
word: "river",
something: "..."
},
{
word: "car",
something: "..."
}
]
What is the best way to do this?
let new_array = [];
for(let item of old_array)
{
new_array.push({word:item, something:"..."});
}
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 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 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 });
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);