This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
i want have a value for a specific key may be nested value also like change value of key1 to value100 or key11 to value111.
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"map1" : {"key4":"value4",
"key5":"value5",
"key6":"value6",
"map2" :
{
"key7":"value7",
"key8":"value8",
"key9":"value9",
"map3" :
{
"key10":"value10",
"key11":"value11",
"key12":"value12"
},
"map4" :
{
"key13":"value13",
"key14":"value14",
"key15":"value15"
}
}
}
}
how to achieve this using javascript or jquery. please help me to find out the issue.
If you stored that in a variable like
var object = { *your JSON here*}
you can access it like:
object.key1
to obtain the value of your key1
If you want to get those in map2, it would be:
object.map2.key7
e.g.
If you want to change values, just do
object.key = *value*;
You can just hold your json on javascript object and edit it.
var _j ={
"key1": "value1",
"key2": "value2",
"key3": "value3",
"map1" : {"key4":"value4",
"key5":"value5",
"key6":"value6",
"map2" :
{
"key7":"value7",
"key8":"value8",
"key9":"value9",
"map3" :
{
"key10":"value10",
"key11":"value11",
"key12":"value12"
},
"map4" :
{
"key13":"value13",
"key14":"value14",
"key15":"value15"
}
}
}
};
_j.key1 = "New Key1";
$('div').html(JSON.stringify(_j));
Here is working JS Fiddle
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
having this type of data
{
"time_1": 20,
"time_2": 10,
"time_3": 40,
"time_4": 30
}
and expected result
[
{
"key1": "time_1",
"key2": 20,
},
{
"key1": "time_2",
"key2": 10,
},
{
"key1": "time_3",
"key2": 40,
},
{
"key1": "time_4",
"key2": 30,
}
]
How to convert object to array using JavaScript? I have updated my question.
You could use the Object.keys() methods to iterate over your object and then use the Array.map() method to transform your object to the array structure you need.
var data = {
"0": "value1",
"1": "value2"
}
var newData = Object.keys(data).map(key => ({
[key]: data[key]
}))
console.log(newData)
--Update--
You would simply need to change the object being returned from the callback function passed to the .map() method.
var data = {
"time_1": 20,
"time_2": 10,
"time_3": 40,
"time_4": 30
}
var newData = Object.keys(data).map(key => ({
"key1": key,
"key2": data[key]
}))
console.log(newData)
given data
const data = {
"time_1": 20,
"time_2": 10,
"time_3": 40,
"time_4": 30
}
get all keys of data object
const KeysOfData = Object.keys(data);
get expected output like this - [
{
"key1": "time_1",
"key2": 20,
},
]
const result = KeysOfData.map(key => {
const value = data[key]
return {
key1: key,
key2: value
}
});
This question already has an answer here:
How do I access an object property dynamically using a variable in JavaScript?
(1 answer)
Closed 2 years ago.
I have this array of objects and I want to show all value in each object:
var array = [{obj1:{property1:"value1",property2:"value2",property3:"value3"}},
{obj2:{property1:"value1",property2:"value2",property3:"value3"}},
{obj3:{property1:"value1",property2:"value2",property3:"value3"}}];
When I try something like this I only get to show the key but not the value
for (let i in array){
for (let key1 in array[i]) {
Any help?
The Object.values() returns an array of values of the Object passed to it.
You can then use flatMap twice to flatten the nested object array and fetch all the values.
var array = [{
obj1: {
property1: "value1",
property2: "value2",
property3: "value3"
}
},
{
obj2: {
property1: "value1",
property2: "value2",
property3: "value3"
}
},
{
obj3: {
property1: "value1",
property2: "value2",
property3: "value3"
}
}
];
function getAllValues(array) {
return array.flatMap(o => Object.values(o))
.flatMap(o => Object.values(o))
}
console.log(getAllValues(array));
I want to rebuild the existing key/value object into another array with only values of original object.
Here is a look.
var car = {"key1": value1, "key2": "value2", "key3": "value3"};
I want to change above javascript object into the following:
var new_car = [value1, value2, value3]
I need your help. Thanks for your time and consideration.
You could use Object.keys and map the values with Array#map.
Caveat: The order may be not guaranteed, because the properties of objects have no order.
var car = {"key1": 'value1', "key2": "value2", "key3": "value3"},
new_car = Object.keys(car).map(function (k) { return car[k]; });
console.log(new_car);
You can simply iterate it over using for :
var car = {"key1": "value1", "key2": "value2", "key3": "value3"};
var new_car = [];
for ( key in car)
{
new_car.push(car[key]);
}
Using lodash/underscore:
var car = {"key1": value1, "key2": "value2", "key3": "value3"};
var new_car = _.values(car);
I have JSON data
var json = {"options": {
"key1": "value2",
"key2": "value2",
"key3": "value3",
}
}
And I want to add one more key with value to it using JavaScript, but I want it to be on the top like this:
var json = {"options": {
"new_key": "new_value",
"key1": "value2",
"key2": "value2",
"key3": "value3",
}
}
How can I do it?
Objects do not guarantee property order in JavaScript, but arrays do. If the order really matters and you are allowed to change your JSON structure, I'd suggest to use an array instead to organize your data.
var json = {
"options": [
{"key": "key1", "value": "value1"},
{"key": "key2", "value": "value2"},
{"key": "key3", "value": "value3"}
]
};
Using this, you could push an element to the start of the array using the unshift method.
json.options.unshift({"key": "new_key", "value": "new_value"});
Although it is not required by any of the ECMAScript specs, nonetheless most implementations do retain the original order of keys in Objects, with the exception of keys with numeric values. You can see a lengthy discussion about the Chrome's implementation here: http://code.google.com/p/v8/issues/detail?id=164
If you want to utilize this, you will need to create a new Object:
var newOpts = {};
newOpts["new_key"] = "new_value";
for (var k in json.options) {
newOpts[k] = json.options[k];
}
json.options = newOpts;
However if you do actually turn your object into a JSON string, and send it to someone else, there is no guarantee that they will preserve the order when they parse the string back into an Object.
I was surprised to discover the widely used Express library for NodeJS actually relies on this behaviour for its format method.
JSON Objects have no order.
If order is important to you, consideer creating options as an array, then you can iterate and adding items in a specific order:
var json = {"options": [
{key: "new_key", value: "new_value"},
{key: "key1", value: "value2"},
{key: "key2", value: "value2"},
{key: "key3", value: "value3"},
]}
You can extend the object in the following way using vanilla JavaScript but again this won't set the order as others have noted.
extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
var json = {
"options": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
};
extend(json.options, {"new_key": "new_value"});
You could use the extend function the ordering of the objects would determine which parameters would be written first, but as stated by DanFromGermany there aren't many real-life cases where the order is relevant.
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I am creating a list by fetching values from a JSON file. It is a nested JSON and the list items are- "Thriller","Fiction" which are basically keys for the next level.
on click of the item I'm passing its name(i.e. Thriller/fiction) to another function...
var val = thriller.
Now I need to fetch the value(i.e. "book" & bookname) corresponding to the passed key in this new function. I'm not able to do so using dot operator-
data.library.val not working..
If anybody has worked on something similar..please help..
JSON:
{ "library": [
{
"Thriller": [
{ "book": "ABC" },
{ "book": "DEF" }
]
},
{
"Fiction": [
{ "book": "GHI" },
{ "book": "JKL" }
]
},] }
Code snippet:
$.getJSON('resources/abc.json', function(data){
var i = data.library;
$("#menuList1").css('display','block');
$(i).each(function(key, value){
$.each(value, function(key, value){
console.log(key);
$("#menuList1").append(''+key+'');
});
}); });
Use data.library[key]
MDN Documentation
Your Json is not valid, this },] specially. A good version :
{
"library": [{
"Thriller": [{
"book": "ABC"
}, {
"book": "DEF"
}]
}, {
"Fiction": [{
"book": "GHI"
}, {
"book": "JKL"
}]
}]
}
you can refer the website http://jsonlint.com for validating your json.