I have data coming in from the db I need to store in a js array of objects. How do I initialize this js array of objects , so that I can keep adding the data when I get it from the db .
The object structure will be of this format
var OsNames = [{deviceName:"TV",
releases: [
{release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
{release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}
]},
{deviceName:"Phone",
releases: [
{release: "rel1", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
{release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}]
}];
var OsNames = [];
You can add data to Array by pushing elements in it when you get it from the db
e.g.
OsNames.push({release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"});
I think that is what you are looking for.
var osNames = [];
// Get Data from Database
var myData = ajax.get(...);
for (var i=0; i < myData.length; i++) {
osNames.push(myData[i]);
}
Related
I was wondering how i could merge these two objects retrieve the tag values and store them in an array. This data is also coming from a json response so incoming data should be pushed onto the end of the new array.
so it would look something like this
["2011 LDI", "2012 LDI"]
array with incoming data:
["2011 LDI", "2012 LDI","2013 LDI"]
Here is what I am getting back in my console.log:
[19-08-25 21:58:32:055 PDT] []
[19-08-25 21:58:32:056 PDT] []
Here are the two objects of arrays i am trying to merge:
{date_added=2019-08-26 04:19:00.112083, tag=LDI 2011}
{date_added=2019-08-26 04:19:00.112089, tag=LDI 2012}
and I want it to look like this
[LDI 2011, LDI 2012]
and how I am trying to do it.
var tagtest = [];
var tags = message.student_detail.student_tags,
i = 0,
len = tags.length;
for (i; i < len; i++) {
var obj = tags[i];
for (a in obj) {
}
Array.prototype.push(tags, tagtest);
Logger.log(tagtest)
}
Based on your desired output ([LDI 2011, LDI 2012]), You may want the only tag values from the array, If this is what you are looking for then .map() will help you
const array = [
{
date_added: '2019-08-26',
tag: 'LDI 2011'
},
{
date_added: '2019-08-26',
tag: 'LDI 2012'
}];
const tags = array.map((r) => {
const chunk = r.tag.split(' ');
return `${chunk[1]} ${chunk[0]}`;
} );
console.log(tags);
A for in loop is a great way to work with objects. I updated the code above so that it was actually an array of objects, not an error. See below :)
var data = [{date_added: "2019-08-26 04:19:00.112083", tag: "LDI 2011"},
{date_added: "2019-08-26 04:19:00.112089", tag: "LDI 2012"}];
var newArr = [];
for(var item in data) {
newArr.push(data[item].tag);
}
console.log(newArr);
I have one array of data and I want to bind that data to multicombobox.
I have binded that data to Multicombobox using javascript as shown below.
The problem is when I select any value from multicombobox, tokens are not displaying as per the default behavior of the control.
Please help me in this. As per my guess, this array is not associative array (do not have keys and values.) this might have caused this issue.
Below is the code:
var data = sap.ui.getCore().getModel("myModel").getData();
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData(data);
this.getView().setModel(oModel, "myModel");
var fruitsArray = data.fruits[0].names;
for(var i = 0; i<fruitsArray.length; i++){
var fruitsDD;
if(!fruitsDD){
fruitsDD = this.getView().byId("MCBId");
}
var oCoreItem = new sap.ui.core.Item();
oCoreItem.setText(fruitsArray[i]);
oCoreItem.setKey(fruitsArray[i]);
fruitsDD.addItem(oCoreItem);
}
{
"A":"Apple",
"B":[
{
"key":1,
"value":"ball",
"fruits":[
"Apple",
"Orange",
"banana",
"Guava"
]
}
]
}
XML view:
<MultiComboBox id="MCBId" width="280px" class="sapUiTinyMarginTop" ></MultiComboBox>
I have a JSON object and within that is a 2D array that has the data I need to visualise in it.
How can I pull only the array stored in 'datapoints' from the following JSON?
var myData = [
{"target": "tar.get",
"datapoints": [
[71.0, 1443793200],
[119.0, 1443793500],
[75.0, 1443793800],
[106.0, 1443794100],
[93.0, 1443794400],
[105.0, 1443794700],
[87.0, 1443795000],
[72.0, 1443795300],
[39.0, 1443795600],
[78.0, 1443795900],
[48.0, 1443796200],
[74.0, 1443796500],
[61.0, 1443796800],
[86.0, 1443797100],
[75.0, 1443797400],
[79.0, 1443797700],
[69.0, 1443798000],
[69.0, 1443798300],
[78.0, 1443798600],
[71.0, 1443798900],
[45.0, 1443799200],
[68.0, 1443799500],
[57.0, 1443799800],
[null, 1443800100]]
}];
Read some beginners guides to JSON like wiki to know the basics.
You can access it like this:
myData[0].datapoints;
You probably want to look through the mydata array like this:
var i;
for (i = 0; i < myData.length; i++) {
var datapoints = myData[index].datapoints;
// do stuff with datapoints
}
You can access JSON members as any JS object:
var datapoints = myData[0].datapoints;
I'm trying to use a for..in loop to iterate through a list of names, add them to a template object ('group'), then add each complete object to an array ('queryList'). This isn't working because each iteration is overwriting ALL values in the array. Any suggestions why this is happening?
// BATTERY OBJECT
var groupList = [ "LOGIN", "BROWSE", "SEARCH"];
// GROUP OBJECT
var group = {dbName: 'CARS', name: '', collectionName: 'group'};
// INIT VARS
var groupName = '',
queryList = [];
// COMPILATION FUNCTION
var buildGroupQueries = function(group){
// BUILD BATCH OF QUERIES
for (var i in groupList){
groupName = groupList[i];
group.name = groupName;
queryList[i] = group;
}
console.log(queryList);
}
buildGroupQueries(group);
It should look like:
[
{"dbName":"CARS","name":"LOGIN","collectionName":"group"},
{"dbName":"CARS","name":"BROWSE","collectionName":"group"},
{"dbName":"CARS","name":"SEARCH","collectionName":"group"}
]
Instead I'm getting:
[
{"dbName":"CARS","name":"SEARCH","collectionName":"group"},
{"dbName":"CARS","name":"SEARCH","collectionName":"group"},
{"dbName":"CARS","name":"SEARCH","collectionName":"group"}
]
You are creating an array of elements referring to the same object, so they all show the same name coinciding with the last time you changed it, which is "SEARCH" in your example.
You have to refer each element to a new object created from the one you want to use as a template.
To do so you can either loop over its properties or clone it as shown below:
// BATTERY OBJECT
var groupList = [ "LOGIN", "BROWSE", "SEARCH"];
// GROUP OBJECT
var group = {dbName: 'CARS', name: '', collectionName: 'group'};
// INIT VARS
var groupName = '',
queryList = [];
// COMPILATION FUNCTION
var buildGroupQueries = function(group){
var i, _group;
// BUILD BATCH OF QUERIES
for (i in groupList){
_group = JSON.parse(JSON.stringify(group));
groupName = groupList[i];
_group.name = groupName;
queryList[i] = _group;
}
console.log(queryList);
}
buildGroupQueries(group);
You modify the group object each time, but you need to modify its copy.
Add this code just after your line for (var i in groupList){
var _group = {};
for (var j in group){ _group[j] = group[j]; }
On each iteration you create a new object and copy to it all properties from the master object.
How can I convert the following data structure:
var data = [ [ { time: 1, speed : 20 } ] ];
to
var data = [ { time: 1, speed: 54 } ];
I just want to remove the array.
As the data is an array, you just want to select the first element of the outer array
so the solution would be
var data = [[{time:1,speed:20}]]; // Or whatever the data is
data = data[0];
Or if you're accessing the data via another object
var data = yourObject[0];
Try this :
JSON.stringify(data).substr(1,JSON.stringify(data).length-2);