Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach to Variable Javascript / React

i want to ask something about how to join data inside variable, so basicly I have data that I foreach (because i need to change that object), then I want to enter each data that I foreach into a variable, as below:

const thisData = {["abcd","efgh"]}
for (let objData of thisData) {
    //lets say i need to change every data from word to number
    //I've managed to change it
    const newData = changetoNumber(objData)
}

then i need to save it to another variable, and now i’m stuck, can you guys help me?

//this is what i need
console.log(listnewData)
//1234 5678

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are looking for a "map".

So, the map method takes in a single parameter, a function which is called on each item, in the simplest case with a single parameter, the item itself like so:


function f(x) {
    return x.length // for example
}

const thisdata = ["abcd", "efg"];
const newvar = thisdata.map(f);
console.log(newvar); // prints [3,2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading