I create multiple objects and push them to the array objArr:
var objArr = [];
var obj = {};
var height = [9,8,7,3,6,5,2,4];
for (var i = 0; i < 8; i++) {
debugger;
var mountainH = height[i];
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
for (var i = 0; i < objArr.length; i++) {
alert(objArr[i].h);
}
But as you can see, each object has the same values. Why?
Put the initialization of obj within your for-loop.
You were re-assigning new values to a global variable obj.
var objArr = [];
var height = [9,8,7,3,6,5,2,4];
for (var i = 0; i < 8; i++) {
debugger;
var obj = {};
var mountainH = height[i];
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
for (var i = 0; i < objArr.length; i++) {
console.log(objArr[i].h);
}
Because the scope of obj in your code is global and it should rather be contained in the for loop.
If you will not declare it inside the loop then the value will get overwritten of the same obj on each iteration instead of a new memory allocation.
var objArr = [];
var height = [9, 8, 7, 3, 6, 5, 2, 4];
for (var i = 0; i < 8; i++) {
debugger;
var mountainH = height[i];
var obj = {};
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
console.log(obj);
As noted, you need to initialize a new object in each iteration of the loop, otherwise all your array members simply share the same object reference.
Additionally, your code can be greatly reduced by building the array using .map(), and fully using the object literal initializer to declare the properties.
var height = [9,8,7,3,6,5,2,4];
var objArr = height.map((n, i) => ({h: n, index: i}));
console.log(objArr);
This is shorter and clearer. For every number in height, it creates a new object and adds it to a new array, which is returned from .map().
It can be even a little shorter with the newer features for object literals.
var height = [9,8,7,3,6,5,2,4];
var objArr = height.map((h, index) => ({h, index}));
console.log(objArr);
Related
I'm trying to loop an array that contains objects and I keep getting and error: "Cannot set property 'color' of undefined". What am I doing wrong?
var ObjectTest = function(something1, something2){
this.Name = something1;
this.Job = something2;
this.color = '';
this.numbers = [];
}
var first = new ObjectTest('Paul', 'teacher');
var second = new ObjectTest('Jane', 'doctor');
var third = new ObjectTest('Mike', 'student');
var someArray = [];
someArray.push(first, second, third);
console.log(someArray);
for(var i =0; i <= someArray.length; i++){
someArray[i].color = 'red';
};
You need to iterate until the length of the array but not over, because indices are zero based
for (var i = 0; i < someArray.length; i++) {
// ^
An array returns undefined for a not existing item. undefined has no property to assign a new value.
var ObjectTest = function(something1, something2) {
this.Name = something1;
this.Job = something2;
this.color = '';
this.numbers = [];
};
var first = new ObjectTest('Paul', 'teacher');
var second = new ObjectTest('Jane', 'doctor');
var third = new ObjectTest('Mike', 'student');
var someArray = [];
someArray.push(first, second, third);
for (var i = 0; i < someArray.length; i++) {
someArray[i].color = 'red';
} // no semicolon here
console.log(someArray);
<= was rong
var ObjectTest = function(something1, something2){
this.Name = something1;
this.Job = something2;
this.color = '';
this.numbers = [];
}
var first = new ObjectTest('Paul', 'teacher');
var second = new ObjectTest('Jane', 'doctor');
var third = new ObjectTest('Mike', 'student');
var someArray = [];
someArray.push(first, second, third);
for(var i =0; i < someArray.length; i++){
someArray[i].color = 'red';
};
console.log(someArray);
Replace <= to < in your loop.
There's only 3 items on the array, meaning you have indexes 0, 1 and 2. The loop should stop when it arrives at at 3. But since you used <= and not <, i <= 3 when i is 3 is true thus executing the code. The error is caused by someArray[3] not existing.
A safer way to loop through arrays without dealing with indexes is to use array.forEach. It only loops as many times as there are items in the array.
someArray.forEach((object, index) => {
object.color = 'red'
})
An easier way to go over an array is to use the forEach.
Something like this:
someArray.forEach(data => data.color = 'red');
Why loop translate variable value to another variable that was equated to one which loop should only change?
let arrr = [1,1,1,1,1];
let preArrr = [0,0,0,0,0];
preArrr = arrr;
for (let i=0; i < arrr.length; i++) arrr[i] = i;
console.log(arrr, preArrr) // arrr = [0,1,2,3,4], preArrr = [0,1,2,3,4]
forEach loop give me the same result
arrr.forEach(function(e, i) { arrr[i] = i })
console.log(arrr, preArrr) // arrr = [0,1,2,3,4], preArrr = [0,1,2,3,4]
but if i change array by instant it wont connect
let arrr = [1,1,1,1,1];
let preArrr = [0,0,0,0,0];
preArrr = arrr;
arrr = [0,1,2,3,4]
console.log(arrr, preArrr) // arrr1 = [0,1,2,3,4], preArrr1 = [1,1,1,1,1];
so, how to avoid this connection, but still use loop? I`m trying to save previous state of the array
Because you changed preArr to point at same array as arr
That doesn't make a copy when you do preArr=arr. Instead both variables are references to the exact same array
If you want to copy the original into preArr use Array#slice()
let arrr = [1, 1, 1, 1, 1];
let preArrr = arrr.slice();
for (let i = 0; i < arrr.length; i++) arrr[i] = i;
console.log('arrr', JSON.stringify(arrr))
console.log('preArrr', JSON.stringify(preArrr))
i noticed that slice() method doesnt prevent reference to the same array, if an array you want to copy has 2 dimentions
to avoid that:
function slice2D(arr) {
let store = [];
for (let i = 0; i < arr.length; i++) {
store.push(arr[i].slice());
}
return store;
}
let pasteArr = slice2D(copyArr);
So I can create a whole bunch of empty objects that are independent with my code below. Is there a better way to do this? Or is this the only method?
var array = [];
for (let i = 0; i < 5; i++)
{
array.push("temp" + i);
}
for (let item of array)
{
eval(`${item} = new Object();`);
}
temp0.first = 1;
temp4.last = "last";
for (let item of array)
{
console.log(eval(item));
}
Simply use an object:
var objects = {};
for (let i = 0; i < 5; i++)
{
objects["temp" + i] = {};
}
Accessing with:
objects["temp0"]
or:
objects.temp0
Or if you want to pollute the global namespace you can use:
for (let i = 0; i < 5; i++)
{
window["temp" + i] = {};
}
And access using:
temp0
temp1 //etc.
or:
window["temp0"] //just like above
Array(5).fill({})
// [{},{},{},{},{}]
Array(5).fill("temp").map((x, i) => x + i)
// ["temp0", "temp1", "temp2", "temp3", "temp4"]
Array.from({length: 5}, (_, i) => "temp" + i)
// ["temp0", "temp1", "temp2", "temp3", "temp4"]
[...Array(5).keys()]
// [0, 1, 2, 3, 4]
It's like this:
function makeMadObjects(num){
var a = [];
for(var i=0; i<num; i++){
a.push({});
}
return a;
}
var arrayOfObjects = makeMadObjects(27);
arrayOfObjects[4].someProperty = 'some value';
console.log(arrayOfObjects);
You can use destructuring assignment to assign variable names to objects within an array, or set property names to objects within an array having dynamic .length, then retrieve specific object from array using index of object within array and property name of object where property is a plain object
let n = 5; // `n`: dynamic; e.g.; Math.floor(Math.random() * 100);
let temps = Array.from({length: n}, (_, i) => ({}));
let len = temps.length;
let [temp0, temp1, temp2, temp3, temp4 /* , ..tempN < len */ ] = temps;
console.log(temp0, temp4);
let n = 5 // Math.floor(Math.random() * 100);
let temps = Array.from({length:n}, (_, i) => ({[`temp${i}`]:{}}));
// set, get `temp0` through `tempN`, when needed for application,
// where `N` is less than `n`, e.g.; set, get `temp0`, `temp4`
let {0:{temp0}, 4:{temp4}} = temps;
console.log(temp0, temp4);
for (var i = 0; i < featureSet.features.length; i++) {
for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
rows["Sensor"] = featureTracts[f].attributes.Sensor;
rows["Resolution"] = featureTracts[f].attributes.Resolution;
rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
// alert(rows);
}
resosat1[i] = rows;
}
i am trying to print all values in resosat1[i] array but it will take only last value and all values overwirite and update only last value to array
for (var i = 0; i < featureSet.features.length; i++) {
var rowAaaray = [];
for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
var rows = {};
rows["Sensor"] = featureTracts[f].attributes.Sensor;
rows["Resolution"] = featureTracts[f].attributes.Resolution;
rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
// alert(rows);
rowAaaray.push(rows);
}
resosat1[i] = rowAaaray;
}
}
Because you are maintaining one variable and overriding it in loop. So you will get last overwritten object only.
I agree with the guy in the comment. Try:
for (var i = 0; i < featureSet.features.length; i++) {
for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
rows["Sensor"] = featureTracts[f].attributes.Sensor;
rows["Resolution"] = featureTracts[f].attributes.Resolution;
rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
resosat1[f] = rows; <======== THIS WILL STORE THE VALUE OF EACH ROW CREATED
}
<==//TRY STORING THE resosat1 array In another array here instead.
//eg. arrayEx[i]=resosat1
//Alternatively, you could use a 2D Array eg. arrayEx[i][f]
}
Hope this helps you out.
try this one..
for (var i = 0; i < featureSet.features.length; i++) {
var arr = []; // create array
for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
var rows = {};
rows["Sensor"] = featureTracts[f].attributes.Sensor;
rows["Resolution"] = featureTracts[f].attributes.Resolution;
rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
// alert(rows);
arr.push(rows);
}
resosat1[i] = arr;
}
}
rows is undeclared (at least in your snippet).
On the other hand, putting var rows = {}, as some suggest, will fix your problem because it creates new object each time. But, if your javascript version accepts it, it would be better to declare it wit let because, this way, you will be really creating new fresh variable (in block scope).
Declaring rows in an outer block will not fix your problem because you will be assigning same object during the whole loop.
Hey i have a simple question i cant find an answer,
i´m trying to generate some raw-data for a chart
lets say i have an array like :
[1,0,0,1,2,0]
is there a way to make an array out of it that has nested arrays that represent the count of duplicate entrys ?
[[0,3],[1,2],[2,1]]
here is some code that does the trick, but saves the count as objects
var array = [1,0,0,1,2,0];
var length = array.length;
var objectCounter = {};
for (i = 0; i < length; i++) {
var currentMemboerOfArrayKey = JSON.stringify(array[i]);
var currentMemboerOfArrayValue = array[i];
if (objectCounter[currentMemboerOfArrayKey] === undefined){
objectCounter[currentMemboerOfArrayKey] = 1;
}else{
objectCounter[currentMemboerOfArrayKey]++;
}
}
but objectCounter returns them like
{0:3,1:2,2:1}
but i need it as an array i specified above ?
for any help, thanks in advance
Try
var array = [1, 0, 0, 1, 2, 0];
function counter(array) {
var counter = [],
map = {}, length = array.length;
$.each(array, function (i, val) {
var arr = map[val];
if (!arr) {
map[val] = arr = [val, 0];
counter.push(arr);
}
arr[1] += 1;
})
return counter;
}
console.log(JSON.stringify(counter(array)))
Demo: Fiddle
You can turn your object into an array easily:
var obj = {0:3,1:2,2:1};
var arr = [];
for (var key in obj) {
// optional check against Object.prototype changes
if (obj.hasOwnProperty(key)) {
arr.push([+key, obj[key]]);
}
}
Note: The object keys are strings, so i converted them back to numbers when placed in the array.
Functional way of doing this, with Array.reduce and Array.map
var data = [1,0,0,1,2,0];
var result = data.reduce(function(counts, current) {
counts[current] = current in counts ? counts[current] + 1: 1;
return counts;
}, {});
result = Object.keys(result).map(function(current){
return [parseInt(current), result[current]];
});
console.log(result);
Output
[ [ 0, 3 ], [ 1, 2 ], [ 2, 1 ] ]
Try:
var data = [1,0,0,1,2,0];
var len = data.length;
var ndata = [];
for(var i=0;i<len;i++){
var count = 0;
for(var j=i+1;j<len;j++){
if(data[i] == data[i]){
count ++;
}
}
var a = [];
a.push(data[i]);
a.push(count);
ndata.push(a);
}
console.log(ndata)
DEMO here.
First you need to map the array to an associative object
var arr = [1,0,0,1,2,0];
var obj = {};
for (var i = 0; i < arr.length; i++) {
if (obj[arr[i]] == undefined) {
obj[arr[i]] = 0;
}
obj[arr[i]] += 1;
}
Then you can easily turn that object into a 2d matrix like so:
arr = [];
for (var k in obj) {
arr.push([k, obj[k]]);
}
alert(JSON.stringify(arr));
Your existing object can be turned into an array with a simple for..in loop. Also your existing code that produces that object can be simplified. Encapsulate both parts in a function and you get something like this:
function countArrayValues(array) {
var counter = {},
result = [];
for (var i = 0, len = array.length; i < len; i++)
if (array[i] in counter)
counter[array[i]]++;
else
counter[array[i]] = 1;
for (i in counter)
result.push([+i, counter[i]]);
return result;
}
console.log( countArrayValues([1,0,0,1,2,0]) );
Demo: http://jsfiddle.net/hxRz2/