how to make an array name a variable in an object - javascript

var data = {};
$('.content').each(function(i){
var id = $(this).attr('id'),
level = $(this).attr('level'),
name = $(this).find('textarea').val(),
data2 = {
i:[
{
"id": id,
"level": level,
"name": name
}
]
};
$.extend(data, data2);
});
In the object data2 i want the array i to be an auto increasing number based on the .each. But it keeps naming it i.

data2 = {};
data2[i] = {
"id": id,
"level": level,
"name": name
};

Property names are treated literally in Javascript, whether or not there are any variables with that name.
However, you redefine the object on every iteration of the loop. Your code should probably look like this:
data2[i] = {
id: id,
level: level,
name: name
};
Note that I have removed the array literal -- the [] around the object literal. I can't imagine you need it, but you could always put it back in if you genuinely did.

Maybe you should do:
data2[i] = [{
"id": id,
"level": level,
"name": name
}];

var data = {};
$('.content').each(function(i) {
var id = $(this).attr('id'),
level = $(this).attr('level'),
name = $(this).find('textarea').val(),
data2 = {};
data2[i] = [{
"id": id,
"level": level,
"name": name}];
};
$.extend(data, data2);

Related

How to create JSON format dictionary?

I would like to format my data with a key to access the information. This is what I currently have:
const dict = [];
dict.push({"student": "Brian", "id":"01", "grade":"Sophomore"})
return dict;
Output:
{
"student":"Brian"
"id": "01",
"grade": "Sophomore"
}
However, I am interested in creating this type of format with my data:
{
"student":"Brian" [
{
"id":"01",
"grade": "Sophomore"
}
]
}
How would I be able to do so? I would like to use "student" as my key to access the rest of its information associated.
You're basically there. However objects always need keys to go along with the values, so you need to assign a key to the object you want to create
const dict = [];
dict.push({
"Brian": {
"id": "01",
"grade": "Sophomore"
}
})
// to retrieve:
let brian = dict.filter(e=>Object.keys(e)[0]==="Brian").flatMap(Object.values)
if (brian && brian.length>0) console.log(brian[0])
//You could also set up your `dict` like this:
const dict2 = {};
dict2["Brian"] = {
"id": "01",
"grade": "Sophomore"
};
//and that would allow you to retrieve your object with
brian = dict2.Brian;
console.log(brian)

Search in json file with Node JS

I have several objects like this and wonder how I can search between these, say that we got the number 1 and want to figure out what the name is.
Should I loop it or what do you guys suggest?
To make things clear, I do want to get the other objects by using the number. Is it possible?
{
"room": [
{
"number": "1",
"name": "room1"
},
{
"number": "2",
"name": "room2"
}
]
}
You could use a map and use the room number as identifier. This way you can access the room data via the room number like that:
var data = {
"room": [
{
"number": "1",
"name": "room1"
},
{
"number": "2",
"name": "room2"
}
]
};
// or load it from a file:
// var data = require('./data.json');
var rooms = new Map();
for(room of data.room) {
rooms.set(Number(room.number), room);
}
// access data for room with number 1
alert(rooms.get(1).name);
Firstly, bring in the file. Make sure to include the './' before the file name to show that it's from the current directory.
const data = require('./data.json');
Now the JSON data is stored inside the variable data. To access the members, use the dot operator. data.room will return the array of rooms.
To access the individual room, use data.room[i] where i is the numbered element in the array (starting from 0). For example, to access the first room:
const first_room = data.room[0];
This first_room variable can now be used to access the name.
console.log(first_room.name);
This will log "room1" to the console.
You can try this. This will help you.
var roomData = {
"room": [
{
"number": "1",
"name": "room1"
},
{
"number": "2",
"name": "room2"
}
]
}
for(key in roomData.room){
console.log("Room Number - " + roomData.room[key].number);
console.log("Room Name - " + roomData.room[key].name);
}

How to push JS object to JSON in new line?

How modified this function to add every object in file's new line?
exports.addWaypoint = function(id, type, param){
var dataIn = fs.readFileSync('./markers.json');
var obj = JSON.parse(dataIn);
obj.markers.push({
"id": id,
"type": type,
"param": param,
});
writeJson(obj);
}
This may be a duplicate of Javascript: How to generate formatted easy-to-read JSON straight from an object?
Here's a quick answer: use JSON.stringify with an optional parameter to indicate the indention for each nested element.
var o = {
"id": 123,
"type": "good",
"param": { name: "Fred", age: 24 },
};
console.log( JSON.stringify(o,null,4) );

Loop over JSON items whether JSON object has multiple arrays or not

I have a working version of a function to loop through a single array in a JSON object, e.g.
[{
"Name": "John",
"Surname": "Johnson"
}, {
"Name": "Peter",
"Surname": "Johnson"
}]
sample function:
function FindName(NameToFind, data1) {
objData = JSON.parse(data1);
for (var i = 0; i < objData.length; i++) {
var Name = objData[i].Name;
if (Name == NameToFind) {
alert("found!");
}
}
}
Now I need to change this function to allow for either single OR multiple arrays e.g.
{
"Table1": [{
"Name": "John",
"Surname": "Johnson"
}, {
"Name": "Peter",
"Surname": "Johnson"
}],
"Table2": [{
"Name": "Sarah",
"Surname": "Parker"
},
{
"Name": "Jonah",
"Surname": "Hill"
}
]
}
Is there a way to determine whether the object has 1 array (like in first example) or more than one arrays (like in 2nd example), and any advice/guidance on how to extend the function to be able to loop through all the items whether it has 1 array or multiple arrays?
Your first object is an array, the second one isn't.
You can test if your argument is an array, or even just test
if (objData[0]) // that's an array
EDIT :
if you want to iterate over all properties of a (just json decoded) object, when it's not an array, you can do this :
for (var key in objData) {
var value = objData[key];
// now use the key and the value
// for example key = "Table1"
// and value = [{"Name":"John","Surname":"Johnson"}, ... ]
}

Javascript nested array transformation

I'm trying to get my head around Javascript array functions. I have a nested array like this, where every row covers the same time period:
[{
"category": "fruit",
"variety": "apple",
"data": [{
"day": 1,
"units": 2
}, {"day": 2,
"units": 4
}]
},{
"category": "fruit",
"variety": "orange",
"data": [{
"day": 1,
"units": 3
}, {"day": 2,
"units": 5
}]
},{
"category": "veg",
"variety": "tomato",
"data": [{
"day": 1,
"units": 4
}, {"day": 2,
"units": 2
}]
}]
I would like to sum the units by day by category, to get an array like this:
[{
"category": "fruit",
"data": [{
"day": 1,
"units": 5
}, {"day": 2,
"units": 9
}]
},{
"category": "veg",
"data": [{
"day": 1,
"units": 4
}, {"day": 2,
"units": 2
}]
}]
I've been tackling this through long looping if statements, and making a bit of a hash of it. Can you see an elegant way to solve this?
Many thanks!
The solution is pretty obvious: Loop through the array, and store the data in a key-value pair. Then, loop through the has, and construct the resulting array using Array.prototype.map. Finally, if you want a nicely formatted JSON-string, use JSON.stringify(result, null, 4);, where 4 is the number of spaced for pretty formatting.
Demo: http://jsfiddle.net/jde6S/
var list = [ ... ];
var hash = {};
for (var i=0; i<list.length; i++) {
var obj = list[i];
// This part makes sure that hash looks like {fruit:[], veg: []}
var hashObjCat = hash[obj.category];
if (!hashObjCat) {
hashObjCat = hash[obj.category] = {};
}
// This part populates the hash hashObjCat with day-unit pairs
for (var j=0; j<obj.data.length; j++) {
var data = obj.data[j];
if (hashObjCat[data.day]) hashObjCat[data.day] += data.units;
else hashObjCat[data.day] = data.units;
}
}
// Now, we hash looks like {fruit: {1:5, 2:9} }
// Construct desired object
var result = Object.keys(hash).map(function(category) {
// Initial object
var obj = {category: category, data:[]};
var dayData = Object.keys(hash[category]);
// This part adds day+units dicts to the data array
for (var i=0; i<dayData.length; i++) {
var day = dayData[i];
var units = hash[category][day];
obj.data.push({day: day, units: units});
}
return obj;
});
// Test:
console.log(JSON.stringify(result, null, 4));
reduce the array to an object (See #RobW's answer on how to do that with loops):
var data = [...] // your input
// Iterate the data with reduce...
var sumsbycategory = data.reduce(function(map, fruit) {
var cat = fruit.category;
// set an property to an object, iterating the days array...
map[cat] = fruit.data.reduce(function(sums, day) {
var d = day.day;
// set or update the units for this day
sums[d] = (sums[d] || 0) + day.units;
return sums; // into the next iteration
}, map[cat] || {}) // ...passing in the already existing map for this cat or a new one
return map; // into the next iteration
}, {}); // ...passing in an empty object
Now we have the following format:
{"fruit":{"1":5,"2":9},"veg":{"1":4,"2":2}}
...which I think is much easier to handle, but lets build your array:
var result = []; // init array
for (var cat in sumsbycategory) { // loop over categories
var data = []; // init array
// add category object:
result.push({category:cat, data:data});
for (var day in sumsbycategory[cat]) // loop over days in category
// add day object
data.push({day:day, units:sumsbycategory[cat][day]});
}
But, wait! An object has no order, and it could happen that day2 comes before days1 in the result array (which might break your appplication?) So, you could use map on the keys of that object which also can be sorted before, to generate the array in one clean-looking expression:
var result = Object.keys(sumsbycategory).map(function(cat) {
return {
category: cat,
data: Object.keys(sumsbycategory[cat])
.sort(function numbercompare(a,b){ return a-b; })
.map(function(day) {
return {
day: day,
units: sumsbycategory[cat][day]
};
})
};
});
result:
[{
"category": "fruit",
"data": [{"day":"1","units":5},{"day":"2","units":9}]
},{
"category": "veg",
"data": [{"day":"1","units":4},{"day":"2","units":2}]
}]
(Demo at jsfiddle.net)
If you're willing to grab some external code and use it to essentially re-index your structure you could probably do something. I know the old dojo data api was a mess to work with, but could allow something like what you seem to be asking.
Personally I'd stick with loops, just keep your variable names readable. Also remember the object literals can be addressed as either an array/hash syntax x[y] or dot syntax x.y

Categories

Resources