This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 1 year ago.
Suppose that I have an object like this (it's a chart js setting).
const radarChartData = {
labels: [3, 4, 6, 7, 8, 8],
data: [0, 0, 0, 0, 0, 0],
bgArr: this.labels.map((label, i) => label === 10 ? "red" : "blue"); // An error has occurred, because this code can't access its own labels in radarChartData.
datasets: [{.....}]
}
const radarChartConfig = {
type: ....,
}
const radarChart = new Chart(radarChartCtx, radarChartConfig)
I tried the following fix, but all failed...
bgArr: radarChartData.labels.map((label, i) => label === 10 ? "red" : "blue");
How to access its own property?
In the first place, it's not a good option to access it and this way?
You can't reference an object during initialization when using object literal syntax. You need to reference the object after it is created.
Only way to reference an object during initialization is when you use a constructor function.
This example uses an anonymous function as a constructor. The new object is reference with this
const data = new (function () {
(this.labels = [3, 4, 6, 7, 8, 8]),
(this.data = [0, 0, 0, 0, 0, 0]),
(this.bgArr = this.labels.map((label, i) =>
label === 10 ? "red" : "blue"
));
})();
const radarChartData = {
labels: data.labels,
data: data.data,
bgArr: data.labels.map((label, i) => (label === 10 ? "red" : "blue")),
};
console.log(radarChartData);
/**
* LOGS
* {
* labels: [ 3, 4, 6, 7, 8, 8 ],
* data: [ 0, 0, 0, 0, 0, 0 ],
* bgArr: [ 'blue', 'blue', 'blue', 'blue', 'blue', 'blue' ]
* }
*/
I have a problem with removing the first element of an array.
To be short, this is how my array looks like if I show it in console:
(11) [Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4)]
0 : (4) ["2017-09-20T16:00:00-07:00", 188.125, 0, 1]
1 : (4) ["2017-09-20T17:00:00-07:00", 123.125, 0, 1]
2 : (4) ["2017-09-20T18:00:00-07:00", 114.25, 0, 1]
3 : (4) ["2017-09-20T19:00:00-07:00", 115, 0, 1]
4 : (4) ["2017-09-20T20:00:00-07:00", 113.25, 0, 1]
5 : (4) ["2017-09-20T21:00:00-07:00", 115.625, 0, 1]
6 : (4) ["2017-09-20T22:00:00-07:00", 114.75, 0, 1]
7 : (4) ["2017-09-20T23:00:00-07:00", 114, 0, 1]
8 : (4) ["2017-09-21T00:00:00-07:00", 112.625, 0, 1]
9 : (4) ["2017-09-21T01:00:00-07:00", 108.375, 0, 1]
10 : (4) ["2017-09-21T02:00:00-07:00", 111.125, 0, 1]
length : 11
__proto__ : Array(0)
I want to remove the first one, 0, I tried using .shift() but didn't work.
Like my array is called myArray.data and I tried myArray.data.shift() and I get this error message:
TypeError: Cannot read property '0' of undefined
at bundle.js:1554
at Function._.map._.collect (vendor.js:11761)
at renderChart (bundle.js:1547)
at bundle.js:1611
at Scope.$digest (vendor.js:34716)
at Scope.$apply (vendor.js:34986)
at bundle.js:259
at vendor.js:14387
at _fulfilled (vendor.js:13992)
at self.promiseDispatch.done (vendor.js:14021)
Any ideas how to solve this?
Later edit:
The code is inside a chart generation function, this is the whole snippet:
data: {
type: chartType,
columns: [
['x'].concat(_.map(myArray.data, function (dataPoint) {
const x = (myArray.data).shift();
console.log(myArray.data);
console.log(x);
return moment(dataPoint[0]).valueOf();
})),
['Expected'].concat(_.map(myArray.data, function (dataPoint) {
return dataPoint[1];
})),
['Actual'].concat(_.map(myArray.data, function (dataPoint) {
return dataPoint[1];
}))
],
x: 'x'
},
I can reproduce the result wanted using Array.shift()
let arr = [
["2017-09-20T16:00:00-07:00", 188.125, 0, 1],
["2017-09-20T17:00:00-07:00", 123.125, 0, 1],
["2017-09-20T18:00:00-07:00", 114.25, 0, 1],
["2017-09-20T19:00:00-07:00", 115, 0, 1],
["2017-09-20T20:00:00-07:00", 113.25, 0, 1],
["2017-09-20T21:00:00-07:00", 115.625, 0, 1],
["2017-09-20T22:00:00-07:00", 114.75, 0, 1],
["2017-09-20T23:00:00-07:00", 114, 0, 1],
["2017-09-21T00:00:00-07:00", 112.625, 0, 1],
["2017-09-21T01:00:00-07:00", 108.375, 0, 1],
["2017-09-21T02:00:00-07:00", 111.125, 0, 1]
];
console.log(arr[0]);
arr.shift();
console.log(arr[0]);
.shift() is working fine.
Problem is in your Array which is going to shift, Check whether myArray.data consists of data, Error says you are trying to shift a value from a undefined (null) object.
splice(0,1) also working fine
You can remove the first array item usingz myArray.data.splice(0, 1) but the error tells that you are trying to apply array method to undefined value. Perhaps the $scope has not received the value yet
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm trying to make an api for a bus ticketing sistem, but I can't seem to get an how to make it work in nodejs
[
{
"id":1,
"hour" : "7:30am"
, "seats" : [
0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0 ,0, 0
]
},
{
"id":2,
"hour" : "9:00am",
"seats" : [
0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0, 0 ,
0, 0, 0 ,0, 0
]
}
This is my mongodb query
db.schedules.update({"id":2}, {"$set" : {"seats.8" : "1"}});
and everything seems to work just fine until I try it on nodejs
router.put('/reserve/:id/:seat', function(req, res, next) {
schedules.update({"id": req.params.id}, { "$set":{ "seats." + req.params.seat+: "1"}}, function(err, doc) {
if (err) {
console.log(err);
return res.status(400).json({"error"});
}
if (doc.result.nModified) {
res.status(200).json({"status": "ok"});
} else {
res.status(400).json({"error": "The seat hasn't been reserved"});
}
});
this is the error returned:
SyntaxError: Unexpected token +
I have tried multiple ways and can't get that working
You have invalid javascript syntax here: schedules.update({"id": req.params.id}, { "$set":{ "seats." + req.params.seat+: "1"}} Can't concat a string in an object definition.
Try like this, using a property reference notation (that will create the property):
let updateObj = { $set: {} }
updateObject.$set['seats.' + req.params.seat] = '1'
schedules.update({"id": req.params.id}, updateObj, function ... )
I'm consuming JSON data that has a bit of a weird structure for example:
{
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
I would like to create some JavaScript that would restructure this data to proper JSON structures so that the "Column" array values become the keys for the "DATA" array's values. So after a JS process is run the data resembles the following:
[
{"ID":7,"name":"Site-A","ENABLED":1,"perms":"1,2","vcenabled":1,"vcvalue":1,"checkenabled":1,"checkvalue":1,"indxenabled":1,"indxvalue":1},
{"ID":15,"name":"Site-B","ENABLED":1,"perms":"1,2","vcenabled":1,"vcvalue":1,"checkenabled":1,"checkvalue":1,"indxenabled":1,"indxvalue":1}
]
What are the JavaScript best practices for accomplishing the JSON restructuring? Could I accomplish this task using a JS framework like JQuery, Foundation JS, ect... ?
Using Underscore, it's a one-liner:
var formatted = _.map(orig.RESULT.DATA, _.partial(_.object, orig.RESULT.COLUMNS));
With plain javascript (less elegant but faster), it would be
var formatted = [],
data = orig.RESULT.DATA,
cols = orig.RESULT.COLUMNS,
l = cols.length;
for (var i=0; i<data.length; i++) {
var d = data[i],
o = {};
for (var j=0; j<l; j++)
o[cols[j]] = d[j];
formatted.push(o);
}
newjson is your new object, j is your json,
code is very fast as it caches the legth and don't uses push.
And as it's pure javascript it's faster than all the libraries.
var j={
"RESULT":{
"COLUMNS":[
"ID",
"name",
"ENABLED",
"perms",
"vcenabled",
"vcvalue",
"checkenabled",
"checkvalue",
"indxenabled",
"indxvalue"
],
"DATA":[
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
var newjson=[],d=j.RESULT.COLUMNS.length;
for(var a=0,b=j.RESULT.DATA.length;a<b;a++){
for(var c=0,tmpObj={};c<d;c++){
tmpObj[j.RESULT.COLUMNS[c]]=j.RESULT.DATA[a][c];
}
newjson[a]=tmpObj;
}
console.log(newjson);
based on Bergi's response u can also use the while-- loop.
var orig={
"RESULT":{
"COLUMNS":[
"ID",
"name",
"ENABLED",
"perms",
"vcenabled",
"vcvalue",
"checkenabled",
"checkvalue",
"indxenabled",
"indxvalue"
],
"DATA":[
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
var formatted = [],
data = orig.RESULT.DATA,
cols = orig.RESULT.COLUMNS,
l = cols.length,
f = data.length;
while (f--) {
var d = data[f],
o = {},
g = l;
while (g--) {
o[cols[g]] = d[g];
}
formatted[f] = o;
}
you can use underscore Array functions for this task
http://underscorejs.org/#arrays
uusing the object function would be helpful
http://underscorejs.org/#object
from the documentation :
_.object(list, [values])
Converts arrays into objects. Pass either a single list of [key, value] pairs, or a list of keys, and a list of values ..the example:
_.object(['moe', 'larry', 'curly'], [30, 40, 50]);
=> {moe: 30, larry: 40, curly: 50}
here is the JSfiddle with the solution
http://jsfiddle.net/rayweb_on/kxR88/1/
and the code looks like this for this specific scenario.
var plain = {
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
},
formatted = [];
_.each(plain.RESULT.DATA, function(value) {
var tmp = {};
tmp = _.object(plain.RESULT.COLUMNS,value)
formatted.push(tmp);
});
console.log(formatted);
Try this using underscorejs.
var plain = {
"RESULT":
{
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
, formatted = [];
_.each(plain.RESULT.DATA, function(value) {
var tmp = {};
_.each(value, function(parameter, pos) {
tmp[plain.RESULT.COLUMNS[pos]] = parameter;
});
formatted.push(tmp);
});
console.log(formatted);
http://jsfiddle.net/kxR88/
Actually, you could use a combination of Array#map for the array and Array#reduce for the objects with the new properties
var data = { RESULT: { COLUMNS: ["ID", "name", "ENABLED", "perms", "vcenabled", "vcvalue", "checkenabled", "checkvalue", "indxenabled", "indxvalue"], DATA: [[7, "Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0], [15, "Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]] }, ERROR: 0 },
result = data.RESULT.DATA.map(function (a) {
return a.reduce(function (o, d, i) {
o[data.RESULT.COLUMNS[i]] = d;
return o;
}, {});
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
With ES6, you could use Object.assign with spread syntax ....
Object.assign adds properties to the given object and returns this object.
Spread syntax ... takes an array and insert the elements as parameters to the function.
var data = { RESULT: { COLUMNS: ["ID", "name", "ENABLED", "perms", "vcenabled", "vcvalue", "checkenabled", "checkvalue", "indxenabled", "indxvalue"], DATA: [[7, "Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0], [15, "Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]] }, ERROR: 0 },
result = data.RESULT.DATA.map(a =>
Object.assign(...data.RESULT.COLUMNS.map((k, i) => ({ [k]: a[i] }))));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Using JQuery:
function jsonToObj(json){
return jQuery.parseJSON(JSON.stringify(json));
}
For example, after a GET request the server send a complex object
$.get("/Files/-2", function (rxData, status) {
var obj = jsonToObj(rxData);
console.log(obj);
});
Logged in console, can be explored through Chrome's Web Developer (F12), in my case looks like this:
image showing nested levels
By simple JS, your solution would look like this:
var yourObj = {
"RESULT": {
"COLUMNS": ["ID","name","ENABLED","perms","vcenabled","vcvalue","checkenabled","checkvalue","indxenabled","indxvalue"],
"DATA": [
[7,"Site-A", 1, "1,2", 1, 1, 1, 0, 0, 0],
[15,"Site-B", 1, "1,2,3,4", 1, 1, 1, 0, 0, 0]
]
},
"ERROR": 0
}
//Solution
var finalARR = [];
var colLength = yourObj.RESULT.COLUMNS.length;
var dataLength = yourObj.RESULT.DATA.length;
for (var i = 0; i < dataLength; i++) {
var finalJSON = {};
for (var j = 0; j < colLength; j++) {
finalJSON[yourObj.RESULT.COLUMNS[j]] = yourObj.RESULT.DATA[i][j];
}
finalARR[i] = finalJSON;
}
console.log(finalARR);