This question already has an answer here:
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
I am trying to add id in a components array. id's will be incrementing based on the no. of components.
component_names = ['test1', 'test2'];
I want to create a new array components which will become :
components = [{id:1, name: 'test1'}, {id:2, name: 'test2'}]
// The ids will increment as the no. of components increase.
What I am trying:
let arr=[];
for(let i=0;i<component_names.length;i++){
arr.push({id:i+1, name: component_names[i]});
}
The above solution works but can I do the same with any of the higher order functions in javascript?
A simple map will work, where parameter 1 is the item and parameter 2 is the index.
let component_names = ['test1', 'test2']
let result = component_names.map((itm, idx) => ({id:++idx,name:itm}))
console.log(result)
let arr = component_names.map((name, index) => ({ name: name, id: index + 1 }));
Related
This question already has answers here:
Converting string array to Name/Value object in javascript
(4 answers)
Closed 4 years ago.
How do I convert a string array:
var names = [
"Bob",
"Michael",
"Lanny"
];
into an object like this?
var names = [
{name:"Bob"},
{name:"Michael"},
{name:"Lanny"}
];
Super simple Array.prototype.map() job
names.map(name => ({ name }))
That is... map each entry (name) to an object with key "name" and value name.
var names = [
"Bob",
"Michael",
"Lanny"
];
console.info(names.map(name => ({ name })))
Silly me, I forgot the most important part
names.map(name => name === 'Bob' ? 'Saab' : name)
.map(name => ({ name }))
Use the Array.map() function to map the array to objects. The map() function will iterate through the array and return a new array holding the result of executing the function on each element in the original array. Eg:
names = names.map(function(ele){return {"name":ele}});
You can do this too:
var names = [
"Bob",
"Michael",
"Lanny"
];
var objNames = []
names.forEach(name => {
objNames.push({
name
})
})
Using ES6 you can set name and it is equal to name: name
you can use the map function.
In general, list.map(f) will produce a new list where each element at position i is the result of applying f to the element at the same position in the original list.
For example:
names.map(function(s) {
return {name: s}
});
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I have next array:
let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
how I can get array without 'param'?
[{name: 'bla'},{name: 'blu'}]
let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
const result = someArr.map(el => el.param);
console.log(result);
let newArray = someArr.map(item => {
return item.param;
});
This question already has answers here:
Keyword 'const' does not make the value immutable. What does it mean?
(5 answers)
Const in JavaScript: when to use it and is it necessary?
(18 answers)
Closed 3 years ago.
let courses = [
{id:1, name : "Femin"},
{id:2, name : "Darsh"},
{id:3, name : "Smit"},
];
let enteredId = 2;
const course = courses.find(c => c.id === enteredId);
course.name = "Darsh Bhimani";
console.log(course);
console.log(courses);
So this is the code I've been working with.
I have been working with Java and C,C++ for the past 5-6 years and started with Javascript a week back. (For node.js).
Now what I am finding confusing here are two things:
The variable course is a constant, still its value can be changed. How ?
The course is fetched from the array courses, but still on changing course, when I log courses, I see that the value of the array has also changed. How is that possible?
In this case it does not matter if the value changes, but when I don't want the array to change, what can I do?
You get with find an object reference and to break this, you could get a shallow copy of the object with Object.assign. This approach works undefined as well, but returns in that case an empty object.
let courses = [
{id:1, name : "Femin"},
{id:2, name : "Darsh"},
{id:3, name : "Smit"},
];
let enteredId = 2;
const course = Object.assign({}, courses.find(c => c.id === enteredId));
course.name = "Darsh Bhimani";
console.log(course);
console.log(courses);
.as-console-wrapper { max-height: 100% !important; top: 0; }
This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 4 years ago.
I have the following array of string that needs to be turn into array of object with the keys value and label
languages: ["Arabic", "Irish"]
0:"Arabic"
1:"Irish"
length: 2
If I wanted to make this array into an array of object like the following:
languages = [
{ value: 'Arabic', label: 'Arabic' }
{ value: 'Irish', label: 'Irish' }
]
How will I do that? Can someone please help?
You can use Array.map():
var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);
You can also use Array.forEach() as:
var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);
This question already has answers here:
Remove array element based on object property
(12 answers)
Closed 8 years ago.
I have array like this
var curChanges = [
{id:1, isChecked:true},
{id:2, isChecked:false},
{id:3, isChecked:true}
];
Now, if i want to remove second array i.e {id:2, isChecked:false} dynamically, How do i do?
Here id is unique.
Thanks in advance.
Firstly, you have a syntax error. Object property values are set with :, not with =:
var curChanges = [
{
id: 1,
isChecked: true
},
// etc...
];
Assuming the order of elements in the array is not fixed, the easiest way to achieve what you're trying to do will be to use the ES5 Array.prototype.filter method:
curChanges.filter(function (elem) {
return elem.id !== 2;
});
If you return true from the filter function, the element will stay in the array. If you return false, it will be removed.
first off, invalid array. Should be : not =
so it becomes:
var curChanges = [ {id:1, isChecked:true}, {id:2, isChecked:false}, {id:3, isChecked:true}];
Use a filter to remove based on id:
curChanges.filter(function(i) { return i.id!=2;})
Or to directly remove the second element :
curChanges.splice(1,1);