Convert plain array to Array of objects [closed] - javascript

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);

Related

Remove element from nested array [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
The community is reviewing whether to reopen this question as of 6 months ago.
Improve this question
I have this data:
const pages = [
[
{id: 1},
{id:2}
],
[
{id: 3},
{id:4}
],
];
What would the best and more efficient way to remove an element from this structure having 2 indexes as params?
e.g. index1 = 1 and index2 = 0 will remove pages[1][0] and pages will be:
const pages = [
[
{id: 1},
{id:2}
],
[
{id:4}
],
];
You can use array.splice to remove an item from an array:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
The splice() method changes the contents of an array by removing or
replacing existing elements and/or adding new elements in place. To
access part of an array without modifying it, see slice().
This is the demo with a function implementing what did you request for:
const pages = [
[
{id: 1},
{id:2}
],
[
{id: 3},
{id:4}
],
];
removeItem(1,0);
console.log(pages);
function removeItem(index1, index2){
pages[index1].splice(index2, 1);
}
Try this :
const idx1 = 1;
const idx2 = 0;
pages[idx1].splice(idx2, 1);

How to filter/search for multiple keys in an object? [closed]

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))

How do I group muiltiple record in array to object in Javascript? [duplicate]

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
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.

Get array of objects from array of objects with specific properties using rxjs angular [closed]

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);

JavaScript: Merge multiple Arrays object in to single Array of objects [closed]

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);

Categories