Convert string Array to Object Array in javascript [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 1 year ago.
Improve this question
I am having the below array
const a1 = [1,2,3];
and want to convert it into
const b1 = [{id:1},{id:2},{id:3}];
Is there a quick way in doing in javascript

Use Array.map()
const a1 = [1,2,3];
const b1 = a1.map(val => ({id: val}));
console.log(b1);

Related

How to convert array of arrays to array of objects in javascript [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 7 months ago.
Improve this question
I have an array like this
var array = [["Fitim",13],["Faltin Developer",1],["Local",1]]
i want to get an object like this:
var totalUsers= [{ Fitim:13},{Faltin Developer: 1},{Local:1}]
how can i do this
You can make use of a simple map function to achieve this:
const userArray = [
["Fitim", 13],
["Faltin Developer", 1],
["Local", 1]
];
console.log(
userArray.map((user) => ({
[user[0]]: user[1],
}))
);

convert a javascript object's value-field to array [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 1 year ago.
Improve this question
suppose the object is obj ={foo:"bla,bla2,bla3",bar:"bla,bla5"}
I want to convert it to 'obj={foo:["bla","bla2","bla3"],bar:["bla","bla5"]}'
Don't know why you want to do that, but here you can just loop through obj with a for...in loop and split the strings by commas:
const obj = {foo:"bla,bla2,bla3",bar:"bla,bla5"};
for (name in obj) {
obj[name] = obj[name].split(',');
}
console.log(obj)

How to Filter an array of strings based on first word in the string in javascript? [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 1 year ago.
Improve this question
With the following array of strings in javascript, is there a way to use the .filter to filter by the first word?
['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two']
For example to .filter(x = 'Desk_*')
['Desk_One', 'Desk_Two', 'Desk_Three']
Maybe this needs to be a regex
You can use startsWith() function like this:
let array = ['Desk_One', 'Desk_Two', 'Desk_Three', 'Chair_One', 'Chair_Two'];
let result = array.filter((item)=>item.startsWith(array[0].split('_')[0]));
console.log(result)

Comparing a string array with object array [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 3 years ago.
Improve this question
I want to compare two arrays
array1=["123',"456"];
array2=[{id":"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name"baak"},{id:"456",name"sxs"}];
my objective is to extract the objects from array2 whose ids match the values in array1.
Can someone help me with the optimal solution?
First of all your second array (array2) is syntactically invalid.
You can try with Array.prototype.filter() and Array.prototype.includes()
var array1=["123","456"];
var array2 = [{id:"001",name:"prashant"},{id:"123",name:"jhh"},{id:"123444",name:"baak"},{id:"456",name:"sxs"}];
var res = array2.filter(i => array1.includes(i.id));
console.log(res);

Key-value to object [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 8 years ago.
Improve this question
I got a key-value object like this:
items = {1:"a",2:"v",3:"u"};
But i want it like this.
items = [{"key":"1","value","a"},{"key":"2","value","v"},{"key":"3","value","u"}];
What is the best way to do this?
I have already tried to do this with $.each.
var items = {1:"a",2:"v",3:"u"};
var newItems = [];
$(items).each(function(k,v){
newItems.push({"key":k,"value":v});
});
And please tell me what is wrong with this question!
Try this-
var items = {1:"a",2:"v",3:"u"};
var obj=[];
for(var i in items){
obj.push({"key":i,"value":items[i]});
}
console.log(obj)

Categories