Key-value to object [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 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)

Related

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 get every element in an array in an if statement js [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 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}`)
})

Store user input in an array or object [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 2 years ago.
Improve this question
I would like to know whether it is easier to store all the data from a text input where the user has to enter their surname in an array or in an object. And how do I do so?.
Thanks in advance
You could do something like this:
var dataArray = [];
function storeData() {
var data = document.getElementById("yourInput").value;
dataArray.push(data);
}
This method uses arrays - personally I find them more easier to use than objects.

How do I get length of my Json 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 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;

How to properly iterate through nested arrays with javascript. [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 8 years ago.
Improve this question
I am having a hard time learning how to iterate through nested arrays, and storing specific array values in javascript.
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
console.log(res);
});
When #btn2 is clicked on, the following is outputed to my console:
If I'd like to access anyone of these values how would I do so? How do I store the value in a variable?
you can iterate through the object and get the value you want:
$('#btn2').click(function(e){
e.preventDefault();
var res = hjs.getValue([1,2,3],['bri', 'alert','name', 'hue', 'sat']);
for(var i in res)
{
if (res.hasOwnProperty(i)) {
var obj = res[i];
console.log('name: ', obj.name);
console.log('sat: ', obj.sat);
}
}
});

Categories