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)
Related
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)
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
So I’m trying to check names through an array and I don’t want to have to use
if (<array name>[0,1,2,3,4,5,6,7])
{ code in here }
depend on what you are trying to do u can use forEach or map
let arr = [1,2,3,4,5,6,7,8,9,10];
arr.forEach((item)=>{
console.log(`${item} is greater than 5 : ${item>5}`)
})
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);
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);
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 7 years ago.
Improve this question
Below is my JSON object
{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}
How do I get length of this.In this example it should return me 3.
My Object name is DecisionObj which have above listed elements.
var string = '{"Decision":[{"recid":"1183","reason":"Approved as Requested","decision":"Approved","approvalamt":"","comment":""},{"recid":"662","reason":"3","decision":"Rejected","approvalamt":"","comment":""},{"recid":"752","reason":"Approved People Resources; But No Funding Approved","decision":"Approved","approvalamt":"","comment":""}]}';
var object = JSON.parse(string);
alert(object.Decision.length);
Arrays have length property. You can use that.
var length = DecisionObj.length;