Accessing Array values dynamically - javascript

I have Array in this format.
rowData[0] = addRow(aa);
rowData[1] = addRow(aaa);
rowData[2] = addRow(aa);
rowData[3] = addRow(aa);
addRow is a function which gets this value process.But i don't want to give the Array Index, instead i want to give rowData[i], then put in a loop and access the elements.
rowData holds the an object which addRow returns.
var data = [rowData];
var table = Ti.UI.createTableView({
data:data
});

Use the Array.push function to store the data:
rowData.push(addRow(aa));
rowData.push(addRow(aaa));
.
.
.
.
.
Another alternative is:
rowData[rowData.length] = addRow(aa);
rowData[rowData.length] = addRow(aaa);
.
.
.
.
.
Use the regular index based iterations to get the data:
for(var i=0; i< rowData.length; i++){
var curItem = rowData[i];
}

for (var i = 0; i < rowData.length; i++)
{
rowData[i] = addRow(aa);
}

did u mean this?
var rowData = {};
rowData[aa] = addRow(aa);
rowData[aaa] = addRow(aaa);
for loop access
for(var index in rowData){
var data = rowData[index]
...
}

A loop may not be feasible in your case. This may be an idea: you can rewrite the Array.push prototype method:
Array.prototype._push = Array.prototype.push;
Array.prototype.push = function(val){ this._push(val); return this;};
After which you can chain the push operations:
rowData.push(addRow(aa))
.push(addRow(aaa))
.push(addRow(aa))
.push(addRow(aa));
But actually, it looks like you are mixing arrays with objects. Here's an answer I formulated earlier on that subject.

Related

Parse Json array push javascript

I have Json Parse list which need to push each category list.
example :
listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
var categoryData = [];
var values = [];
for(var i=0;i<listChartPeriods.length;i++){
categoryData.push(listChartPeriods.slice(0,1)[0]); //here need to push each date
values.push(listChartPeriods[i])
}
expected out put:
categoryData=["2018-05-04","2018-05-11","2018-05-18","2018-05-25","2018-06-01"]
values=[21807210.5028442,21807210.5028442,21807210.5028442]//each category values
Just use Object.keys to get the dates in the array.
const listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
var categoryData = Object.keys(listChartPeriods);
console.log(categoryData);
Below should get the job done for you. A for in loop is your friend when it comes to working with objects.
var listChartPeriods={"2018-05-04":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-11":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-18":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-05-25":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442],"2018-06-01":[21807210.5028442,21807210.5028442,21807210.5028442,21807210.5028442]}
var categoryData = [];
for(var char in listChartPeriods){
for(var i = 0; i < listChartPeriods[char].length; i++){
categoryData.push(listChartPeriods[char][i]);
}
}
console.log(categoryData);
EDIT: Just read your updated question and you are only wanting the key names. You can also do this with a for in loop.
for(var char in listChartPeriods){
categoryData.push(char)
}
console.log(categoryData);
Following the solution:
for (let date in listChartPeriods){
categoryData.push(date);
let [first] = listChartPeriods[date];
values.push(first);
}
categoryData = ["2018-05-04", "2018-05-11", "2018-05-18", "2018-05-25", "2018-06-01"]
values = [21807210.5028442, 21807210.5028442, 21807210.5028442, 21807210.5028442, 21807210.5028442]

create array using push method

var members = [
['Fred G. Aandahl', '1951-1953', 'North Dakota'],
['Watkins Moorman Abbitt', '1948-1973', 'Virginia'],
];
I need to create like this dynamically, I am using the following code to do that:
var data = new array();
var members = [];
$.each(data, function (i, elem) {
data.push(elem["p_age"], elem["p_name"], elem["p_date"]);
members.push(data);
});
console.log(members);
}
I need to print this values, for that.
for(var x = 0; x < members.length; x++) {
console.log(members[i][0]);
console.log(members[i][1]);
console.log(members[i][2]);
}
so when i try this i get following.
[object][object][object][object][object][object]
I am not sure how is your code working! It has some error's if your already aware of.
Your code should work fine after you change to:-
var data = new Array();//Was an Error in your code
var members = [];
$.each(temp, function (i, elem) {
data.push(elem["p_age"], elem["p_name"], elem["p_date"]);
members.push(data);
});
console.log(members);
for (var x = 0; x < members.length; x++) {
console.log(members[x][0]);//Was an Error in your code
console.log(members[x][1]);
console.log(members[x][2]);
}
Secondly, how does data.push(elem["p_age"], elem["p_name"], elem["p_date"]); works for you? It should give you undefined.
Just to get myself clear I wrote down your code to a fiddle. Have a look.
Try
var members=[];
$.each(data, function(i, elem) {
members.push([elem["p_date"],elem["p_name"],elem["p_date"]]);
});
console.log(JSON.stringify(members))
This looks suspect:
$.each(data, function(i, elem) {
data.push(elem["p_date"],elem["p_name"],elem["p_date"]);
It looks like you're trying to iterate over data, pushing the elements back on to data. I imagine that the $.each() needs to iterate over something else.
I also question why you're pushing elem['p_date'] onto an array twice.
Because it is treating them as objects. Try using toString() method.
Hi use x instead of i for loop.
for(var x=0;x<members.length;x++){
console.log(members[x][0]);
console.log(members[x][1]);
console.log(members[x][2]);
}
It will work.
Not
var data=new array();
but
var data=new Array();
Array's class name is Array, but not 'array'.

how to add properly the value of counter to a variable name

it can seems easy, but I'm little lost...
What I need is to add the counter value to a variable name.
for (var i=0; i<8; i++){
var upBt0; //this is the name in the first iteration
var upBt1; //this is the name in the second iteration
.
.
.
var upBt8; //this is the name in the last iteration
}
How can I do this properly?
Sorry, Daniel
EDIT:
for (var i=0; i<8; i++)
{
this.upBt = "upBt"+i;
this.upBt = new PL_Button().init("upBarButton"+i);
}
I create buttons...in particular 8 buttons...
And later, I need access to each of these buttons:
function (){
this.upBt1;
this.upBt1;
this.upBt3;
this.upBt6;
}
Hope have explained better.
EDIT 2:
Finally, I solved it using an auxiliar array of the class, where I pushed each object in each iteration. Each item of this array is a real reference to each object, so changed the item in the array, changes are also made in the corresponding object...
Hope have explained well.
Thanks for your help,
Daniel
You can use this, but it's ugly code...
for (var i=0; i<8; i++){
this["upBt" + i] = Object.create(null);
}
Use an array helped to me:
this._arrayButtons = {};
for (var i=0; i<8; i++)
{
this.upBt = new PL_Button().init("upBarButton"+i);
this.upBt.imgPath = "res/builder/"+upBarImgs[i];
.
.
.
this._arrayButtons[i] = this.upBt;
}
After, in one function, I can access to the content of the variables such as:
function refreshFrameAlpha(){
this._arrayButtons[3].alpha = 125;
this._arrayButtons[5].alpha = 225;
.
.
}
In this way, I can refresh alpha (p.e) of the object, because each item of the array is a reference to the corresponding object.

How to "clean" an array in javascript?

I am stuck here. How can I clean this array:
{"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]}
So that it looks like:
["5201521d42","52049e2591","52951699w4"]
I am using Javascript.
You just need to iterate over the existing data array and pull out each id value and put it into a new "clean" array like this:
var raw = {"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
var clean = [];
for (var i = 0, len = raw.data.length; i < len; i++) {
clean.push(raw.data[i].id);
}
Overwriting the same object
var o = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
for (var i = o.data.length; i--; ){
o.data[i] = o.data[i].id;
}
What you're doing is replacing the existing object with the value of its id property.
If you can use ES5 and performance is not critical, i would recommend this:
Edit:
Looking at this jsperf testcase, map vs manual for is about 7-10 times slower, which actually isn't that much considering that this is already in the area of millions of operations per second. So under the paradigma of avoiding prematurely optimizations, this is a lot cleaner and the way forward.
var dump = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
var ids = dump.data.map(function (v) { return v.id; });
Otherwise:
var data = dump.data;
var ids = [];
for (var i = 0; i < data.length; i++) {
ids.push(data[i].id);
}
Do something like:
var cleanedArray = [];
for(var i=0; i<data.length; i++) {
cleanedArray.push(data[i].id);
}
data = cleanedArray;
Take a look at this fiddle. I think this is what you're looking for
oldObj={"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
oldObj = oldObj.data;
myArray = [];
for (var key in oldObj) {
var obj = oldObj[key];
for (var prop in obj) {
myArray.push(obj[prop]);
}
}
console.log(myArray)
Use Array.prototype.map there is fallback code defined in this documentation page that will define the function if your user's browser is missing it.
var data = {"data":[{"":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
var clean_array = [];
for( var i in data.data )
{
for( var j in data.data[i] )
{
clean_array.push( data.data[i][j] )
}
}
console.log( clean_array );
You are actually reducing dimension. or you may say you are extracting a single dimension from the qube. you may even say selecting a column from an array of objects. But the term clean doesn't match with your problem.
var list = [];
var raw = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]};
for(var i=0; i < raw.data.length ; ++i){
list.push(raw.data[i].id);
}
Use the map function on your Array:
data.map(function(item) { return item.id; });
This will return:
["5201521d42", "52049e2591", "52951699w4"]
What is map? It's a method that creates a new array using the results of the provided function. Read all about it: map - MDN Docs
The simplest way to clean any ARRAY in javascript
its using a loop for over the data or manually, like this:
let data = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},
{"id":"52951699w4"}]};
let n = [data.data[0].id,data.data[1].id, data.data[2].id];
console.log(n)
output:
(3) ["5201521d42", "52049e2591", "52951699w4"]
Easy and a clean way to do this.
oldArr = {"data":[{"id":"5201521d42"},{"id":"52049e2591"},{"id":"52951699w4"}]}
oldArr = oldArr["data"].map(element => element.id)
Output: ['5201521d42', '52049e2591', '52951699w4']

How to separate the values from two dimension array in js?

jQuery.get("ChkNewRspLive.php?lastmsgID=" + n, function(newitems){
//some code to separate values of 2d array.
$('#div1').append(msgid);
$('#div2').append(rspid);
});
Let's say the value of newitems is [["320","23"],["310","26"]]
I want to assign "320" and "310" to var msgid.
I want to assign "23" and "26" to var rspid.
How to do that?
I tried to display newitems and the output is "Array". I tried to display newitems[0] and the output is blank.
If I redeclare var newitems = [["320","23"],["310","26"]]; it works. So I guess the variable newitems from jQuery.get is something wrong. Is it I cannot pass the array from other page to current page through jQuery directly?
Regarding the array on other page, if echo json_encode($Arraytest); the output is [["320","23"],["310","26"]] but if echo $Arraytest; the output is Array. How do I pass the array from other page to currently page by jQuery.get?
I don't totally understand the question but I'm going to assume you want the values in an array, as two values can't be stored in one (scalar) variable simultaneously.
jQuery.get("ChkNewRspLive.php?lastmsgID=" + n, function(newitems){
//some code to separate values of 2d array.
var msgid = [],
rspid = [];
for( i = 0 ; i < newitems.length ; i++){
msgid[msgid.length] = newitems[i][0];
rspid[rspid.length] = newitems[i][1];
}
//msgid now contains ["320","310"]
//rspid now contains ["23","26"]
});
Bear in mind those are in the function scope. If you want to use them outside of that scope instantiate them outside. see: closure
You can use pluck from underscore.js: http://documentcloud.github.com/underscore/#pluck
var msgid = _(newitems).pluck(0)
var rspid = _(newitems).pluck(1)
Try this:
function getArrayDimension(arr, dim) {
var res = [];
for(var i = 0; i < arr.length; i++) {
res.push(arr[i][dim]);
}
return res;
}
var newitems = [["320","23"],["310","26"]];
var msgid = getArrayDimension(newitems, 0);
var rspid = getArrayDimension(newitems, 1);
msgid and rspid are arrays holding the 'nth' dimention.
Tnx

Categories