This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
Closed 6 years ago.
I'm trying to convert a JSON key value object to an Array, but I'm not sure how to get it in the format I need. What I have for the JSON is something similar to below:
{
"01": "yes",
"02": "yes",
"03": "no"
}
but I need an Array like the one below so I can iterate through it easily:
["01:yes","02:yes","03:no"]
or is it possible to iterate through this JSON object while accessing the keys and values easily?
Use Array#reduce
Object.keys() returns an array of a given object's own enumerable properties
var obj = {
"01": "yes",
"02": "yes",
"03": "no"
};
var op = Object.keys(obj).reduce(function(a, b) {
return a.concat(b + ':' + obj[b]);
}, []);
console.log(op);
Related
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 2 years ago.
I have an object 'myObjects':
let myObjects = {
"obj1":["X1","1.2"],
"obj2": "2",
"obj3": "3"
};
I am trying to only remove the X1 from the obj1 section. I have tried doing:
delete obj1[0];
however end up with
[null,"1.2"]
how do I get rid of the null?
You are going to remove element from Array not Object. In that case, you can use Array.splice as follows.
let myObjects = {
"obj1":["X1","1.2"],
"obj2": "2",
"obj3": "3"
};
delete myObjects.obj1.splice(0, 1);
console.log(myObjects);
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Closed 3 years ago.
I would like to convert a javascript array which looks like:
['https://www.google.com', 'https://www.facebook.com']
to a list of JSON objects that looks like this:
[{"redirectUri": "https://www.google.com"},
{"redirectUri": "https://www.facebook.com"}]
I have tried using Object.assign({}, array);
however this retuns json with the parameter name as the index of the array value and are all in a single object:
{"0": "https://www.google.com", "1": "https://www.facebook.com"},
is there a way to change this to use a custom parameter name dynamically?
You just need to map your elements respectively, using Array.map() method:
let result = arr.map(o => {
return {
"redirectUri": o
}
});
Demo:
let arr = ['https://www.google.com', 'https://www.facebook.com'];
let result = arr.map(o => {
return {
"redirectUri": o
}
});
console.log(result);
This question already has answers here:
Get array of object's keys
(8 answers)
Closed 4 years ago.
I am currently trying to handle a GET request that returns a body in application/json structured like this:
{
"items": {
"item001": {"id":1234, "name": "name001"},
"item002": {"id":1235, "name": "name002"},
"item003": {"id":1236, "name": "name003"}
}
}
I'm trying to get an array of strings that looks like
array = ["item001", "item002", "item003"];
I don't care about any of the underlying hierarchy, I just need the key of each object as an array. I've tried several different methods (map(), JSON.stringify(), etc) but I can't seem to index each key in a array[i] format.
In fact, each time I try to even print the name of a single key, for example
var obj = JSON.parse({'whole json here'});
print(obj.items[1]);
I get an [object Object] error, which makes sense as obj.items is not indexed with a key other than "item001" for example. However, I do not know what the key for each object will be, hence the need for an array of the keys. Thank you in advance!
You can do Object.keys.It will return an array of the keys of an object
var x = {
"items": {
"item001": {
"id": 1234,
"name": "name001"
},
"item002": {
"id": 1235,
"name": "name002"
},
"item003": {
"id": 1236,
"name": "name003"
}
}
}
var y = Object.keys(x.items);
console.log(y)
You can use Object.keys(). For Reference
var obj={"items":{"item001":{"id":1234,"name":"name001"},"item002":{"id":1235,"name":"name002"},"item003":{"id":1236,"name":"name003"}}};
var result = Object.keys(obj.items);
console.log(result);
Object.keys will do that.
var obj = JSON.parse({'your json'});
console.log( Object.keys(obj.items) );
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 8 years ago.
Problem: Create a javascript function that takes an object (of any size and depth), iterates through it and runs some basic string replacing on any strings and returns the object with the amended values.
I have two ideas about the implementaion, but cannot get a solution for either:
var context = {
"test1": "123",
"test2": "123",
"test2.2": "123",
"test3": {
"test4": "cats",
"test5": {
"test6": "test1",
"test123": "1231232"
}
}
};
Idea 1)
Loop the array, and change the values,
http://php.net/manual/en/language.references.pass.php
In some way similar to PHP
Idea 2)
Build an array of path(s) to the object, so to replace the "test123" value I can create such an array:
['test3', 'test5', 'test123']
... this part is simple, but how do I then convert this to something like:
context['test3']['test5']['test123'] ?
Thankyou in advance.
Loop over the object and invoke the function recursively if the value at hand is an object. In pseudocode:
function replaceInObject ( obj, find, repl)
for key in obj
value = obj[key]
if value is object
obj[key] = replaceInObject(value, find, repl)
else
obj[key] = value.replace(find, repl)
return obj
This question already has answers here:
Sort JavaScript object by key
(37 answers)
Sorting object property by values
(44 answers)
Closed 9 years ago.
I have an object that looks like:
{
"2013": {
"name": 2013,
"ts_create": 1375254000000,
"view_url": "some_url",
"save_url": "some_url_2",
"id": "",
"exists": true,
"children": []
},
"2012": {
"name": 2013,
"ts_create": 1375254000000,
"view_url": "some_url",
"save_url": "some_url_2",
"id": "",
"exists": true,
"children": []
},
"2011": {
"name": 2013,
"ts_create": 1375254000000,
"view_url": "some_url",
"save_url": "some_url_2",
"id": "",
"exists": true,
"children": []
}
}
The problem I am facing is, it seems only in Google Chrome that when I loop over this object the objects loop backwards.
The expected result of looping over and displaying the object should list out things from 2013, 2012, 2011. Which happens in Firefox.. However no change in the method, same exact object in Chrome.. I will get 2011, 2012, 2013
So with this I need to obviously apply some level of sorting over this object before looping over it, seeing as Chrome wants to get the loop to read over it backwards. Problem I am facing with that is not entirely sure how to sort an object with JS
Instead of property/value pair implement this structure as an array of objects. Arrays you can sort by any property of objects it contains.
Because objects are unordered collections, it's up to you to enforce the ordering you want.
One way is to get an Array of the object properties, sort them, then iterate the Array.
Object.keys(data)
.sort(function(a, b) {
return +b - +a;
})
.forEach(function(key) {
var item = data[key];
});
This assumes that you wanted a simple descending order.
When you iterate over object properties, there is no guarantee on the order. You can add your objects to an array, and sort the array instead:
var obj = {"2012" : {}, "2011" : {}, "2013" : {}};
var arr = [];
for (var year in obj) {
arr.push({year : year, data : obj[year]});
}
arr.sort(function(a,b) {
return b.year - a.year;
});
http://jsfiddle.net/wK5KE/
One way to get the keys in sorted order would be to use the method defined here to generate an array of the keys:
Getting JavaScript object key list
Then you could use the javascript sort function:
http://www.w3schools.com/jsref/jsref_sort.asp
Or write your own if you feel inclined.
Then just loop through the array of keys, using object[key] to extract the value.