Append element in JSobject - javascript

I am trying to create a JS object which has this structure
{ node1:
[ 'test1.1',
'test1.2'],
node2:
['test2.1',
'test2.2']
}
This is my code
for (var k = 0; k < keys.length; i++){
key = keys[k];
var result = {};
var r = [];
for (var i = 0; i < elements.length; i++){
r.push(elements[i]);
}
result[key] = r;
}
the result looks a bit different from my expectation and is not a valid JSON:
{ node1:
[ 'test1.1',
'test1.2'] }
{ node2:
[ 'test2.1',
'test2.2' ] }
I am not sure what is wrong about the code.

Declare var result = {}; outside the for loop and it will work as currently a new object is created inside the loop.
var result = {};
for (var k = 0; k < keys.length; k++) {
key = keys[k];
var r = [];
for (var i = 0; i < elements.length; i++) {
r.push(elements[i]);
}
result[key] = r;
}
You also have i++ in the first loop so change that to k++ otherwise there will be a infinite loop.

Related

unable to copy 2015,2016,2017 to values property of 2015,2016,2017 of arr

I tried to copy 2015,2016,2017 to values property of value of 2015,2016,2017 of respected year
expected output as below
arr = [{2015:1,2016:2,2017:3, values: [{id: "111",year: 2015,value:1},{id: "991",year: 2016,value: 2},{id: "123",year: 2017,value: 3}]}];
any suggestion?
please refer below snippet
let arr = [{2015:1,2016:2,2017:3, values: [{id: "111",year: 2015,value:20},{id: "991",year: 2016,value: 40},{id: "123",year: 2017,value: 60}]}];
console.log("---arr--before", arr)
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].values.length; j++) {
arr[i].values[j].value = arr[i][2015];
arr[i].values[j].value = arr[i][2016];
arr[i].values[j].value = arr[i][2017];
}
}
console.log("---data--after", arr)
Ok I will play...
After major changes to the question here's a solution:
let arr = [{2015:1,2016:2,2017:3, values: [{year: 2015,value:20},{year: 2016,value: 40},{year: 2017,value: 60}]}];
console.log("---arr--before", arr)
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].values.length; j++) {
propName = arr[i].values[j].year;
arr[i].values[j].value = arr[i][propName];
}
}
console.log("---arr--after", arr)
I leave you two options, it depends on how you are going to work the object will come better one or the other.
You were missing the loop that reads y2015
let arr = [{y2015:1,y2016:2,y2017:3, values: [{y2015:20},{y2016: 40},{y2017: 60}]}];
console.log("---arr--before", arr)
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].values.length; j++) {
let k = Object.keys( arr[i].values[j] )[0];
arr[i].values[j][k] = arr[i][k];
}
}
console.log("---arr--after", arr);
let arr2 = [{y2015:5,y2016:6,y2017:7, values: {y2015:50, y2016: 60, y2017: 70}}];
console.log("---arr--before2", arr2);
for (let i = 0; i < arr2.length; i++) {
for (let j in arr2[i].values){
arr2[i].values[j] = arr2[i][j];
}
}
console.log("---arr--after2", arr2);
Try this:
let arr = [{2015:1,2016:2,2017:3, values: [{id: "111",year: 2015,value:20},{id: "991",year: 2016,value: 40},{id: "123",year: 2017,value: 60}]}];
console.log("---arr--before", arr)
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].values.length; j++) {
arr[i].values[j].value = arr[i][Object.keys(arr[i])[j]];
}
}
console.log("---arr--after", arr)

How do Object and some Key in square brackets work?

Can somebody explain how does seen[item] work?
var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];
function uniq_fast(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
for(var i = 0; i < len; i++) {
var item = a[i];
if(seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
}
var e = uniq_fast(a);
console.log(e);
It returns undefined everytime:
var a = [1,1,1,1,1,2,2,3,4,4,4,4,4,5,"a",'b','a',"c"];
for(var i = 0; i < a.length; i++) {
var seen = {};
var item = a[i];
var x = seen[item];
console.log(x);
}
I've added comments to your code, you can also check google Developer tools (or other browser equivalent) set breakpoint before for loop and go step by step to see the value of each variable.
var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];
function uniq_fast(a) {
var seen = {};
var out = [];
var len = a.length;
var j = 0;
// if i == 0;
for(var i = 0; i < len; i++) {
var item = a[i]; // item is "a" because that's first item in array
if(seen[item] !== 1) {
seen[item] = 1; // seen is {"a": 1} it's the same as seen['a'] = 1
out[j++] = item;
}
}
return out;
}
in your second code:
var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];
for(var i = 0; i < a.length; i++) {
var seen = {}; // you have empty object
var item = a[i]; // item is "a"
console.log(seen[item]); // this is undefined because seen["a"] is empty,
// you never adding anything to seen
}
In essence it's not more than
object['a'] = object.a

possible combinations get a targetTotal counting itself as a case

Trying to count possible combinations to get a targetTotal. Using powerSet returns the sum without adding itself. E.g [1,2,3,5] returns [3+1] for a targetSum of 4, whereas I expect to get [1+1+1+1], [2+2], [3+1].
Do you have any ideas how I could make it count itself first as a case?
function powerset(arr) {
var ps = [[]];
for (var i=0; i < arr.length; i++) {
for (var j = 0, len = ps.length; j < len; j++) {
ps.push(ps[j].concat(arr[i]));
}
}
return ps;
}
function sum(arr) {
var total = 0;
for (var i = 0; i < arr.length; i++)
total += arr[i];
return total
}
function findSums(numbers, targetSum) {
var sumSets = [];
var numberSets = powerset(numbers);
for (var i=0; i < numberSets.length; i++) {
var numberSet = numberSets[i];
if (sum(numberSet) == targetSum)
sumSets.push(numberSet);
}
return sumSets;
}
Example invocation:
findSums([1,2,3,4,5],6); [[2,3], [1,4], [5], [1,1,1,1,1,1], [2,2,2], [3,3]]

Undefined Array

i am trying to make a for loop out of a var variable but it doesn't work. For some reason tempArray.length is always undefined, it never return anything different. Can anyone help ?
for (i = 0; i < arr.length; i++) {
tempArray = arr[i];
for (k = 0; k <= tempArray.length; i++) {
if (tempArray[k] != /[0-9]+/) {
countinue;
}
var tempArray = [];
for (i = 0; i < arr.length; i++) {
tempArray = arr[i];
}
for (k = 0; k <= tempArray.length; i++) {
if (tempArray[k] != /[0-9]+/) {
countinue;
}
}
Temp array needs to be set to an array.
Loop through and build tempArray first, then loop through the temp array and do what ever you need to.

Javascript - count and remove from an object

I have an object with duplicate values and I want to count all those which have the same value and remove them.
var myArray = [{nr: 'bbc',}, {nr: 'bbc'}, {nr: 'bbc'}, {nr: ccc}];
from this array I want to create another array but remove the duplicated values and count them to be like this.
var myArray = [{nr: 'bbc',amount: 3}}, {nr: ccc,amount: 1}];
You could probably use a better format
var count = {};
for(var i = 0; i < myArray.length; ++i) {
if(typeof count[myArray[i].nr] == 'undefined') {
count[myArray[i].nr] = 0;
}
++count[myArray[i].nr];
}
and this wound yield somehing like:
count = {
bcc: 3,
ccc: 1
};
if you still need it with the structure you specified, then:
var newArray = [];
for(var k in count) {
newArray.push({
nr: k,
amount: count[k]
});
}
If you want the same structure, this will work for you
var newArray = [];
for (var i = 0; i < myArray.length; i++) {
var matched = false;
for (var j = 0; j < newArray.length; j++) {
if(myArray[i].nr === newArray[j].nr){
matched = true;
newArray[j].amount++;
break;
}
};
if(!matched)
newArray.push({nr:myArray[i].nr,amount:1});
};
console.log(newArray);

Categories