0

I have a two nested forEach loops, i tried using standard forEach and some own logic but it failed, than i tried the async library from npm with the code bellow:

function addValue(data, callback){
    var final_data = [];
    data.forEach(element => {
        final_data.push(element);
    });
    var values = 
    [[1,0],
    [2,0],
    [3,0],
    [-1,0],
    [-2,0],
    [-3,0],
    [1,1],
    [2,1],
    [3,1],
    [-1,1],
    [-2,1],
    [-3,1]];
    async_.forEachOf(final_data, (item, {}, callback1) => {
        values.forEach(value=>{
            var obj = Object.assign({},item);
                    obj.x+=value[0];
                    obj.y+=value[1];
                    final_data.push(obj);
        });
        callback1();
    }, err => {
        if (err) console.error(err.message);
        // Here i get different values for separate executions with same data
        console.log(final_data.length)
    });
}

What i am trying to achieve is that final_data is an array of objects, the first forEachOf loop should iterate through the final_data object and for each of them should run the second nested forEach creating a new object with modified values and pushing it back in the final_data array of object. The problem i am encountering is that the function always returns at a different time, hence the lenght of the final_data is always different, once 100 objects get pushed, once 670. So basically the callback in the main function which is addValue should be called once all of the objects are processed and the two for loops finish...

Using non-async code end up in infinite loop, strange

function addValue(data, callback){
    var final_data = [];
    data.forEach(element => {
        final_data.push(element);
    });
    var values = 
    [[1,0],
    [2,0],
    [3,0],
    [-1,0],
    [-2,0],
    [-3,0],
    [1,1],
    [2,1],
    [3,1],
    [-1,1],
    [-2,1],
    [-3,1]];
    for(var i=0; i<final_data.length; i++){
        for(var j=0; j<values.length; j++){
            var obj = Object.assign({},final_data[i]);
            obj.x+=values[j][0];
            obj.y+=values[j][1];
            final_data.push(obj);
            console.log(final_data.length)
        }
    }
}

EDIT 2: Using forEach with no async works... What might be the cause of the infinite standard loop, versus the forEach ?

2
  • There doesn't seem to be any async code here, so is there any reason your trying to do async coding.? Commented Apr 23, 2019 at 9:27
  • Do you want to call the callback with new obj and get the result?Is that what you need? Commented Apr 23, 2019 at 9:38

2 Answers 2

1

You can try this assuming you have es6 support

function addValue (data) {
 var values = 
[[1,0],
[2,0],
[3,0],
[-1,0],
[-2,0],
[-3,0],
[1,1],
[2,1],
[3,1],
[-1,1],
[-2,1],
[-3,1]];
var finalData = data.map((item)=>{
   return values.map(function(value) {
        var obj = Object.assign({},item);
                obj.x+=value[0];
                obj.y+=value[1];
        return obj;         
   })  
});
return finalData.flat(2);
}
Sign up to request clarification or add additional context in comments.

Comments

0
async function addValue(data, callback){
var final_data = [];
data.forEach(element => {
    final_data.push(element);
});
var values = 
[[1,0],
[2,0],
[3,0],
[-1,0],
[-2,0],
[-3,0],
[1,1],
[2,1],
[3,1],
[-1,1],
[-2,1],
[-3,1]];
var result =[]
for(item of final_data)
   for (const value of values) {
     var obj = Object.assign({},item);
                obj.x+=value[0];
                obj.y+=value[1];
          await result.push(obj);
   }

6 Comments

In order to use await for callback1, i have to use async inside the forEachOf too right? Since otherwise it will throw error that await is valid only in async functions even though the higher scope function is async
@nivendha The purpose of the code is for each element of the final_data array to create N number of different copies of it and push it back to the main final_data array and return it to where ever the function addValue() is called...
@KristijanStefanoski So what is is you think requires any of this to use promises.? Just return final_data;, you don't even need to use a callback..
@KristijanStefanoski this doesnt need an async at all i think
Okay, so i used non-async and i end up in an infinite loop for some reason, you can check update above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.