Home > Blockchain >  Mapping a string into a Object Array
Mapping a string into a Object Array

Time:11-11

I am fairly new to Javascript is there a way which we can achieved the following result.I need to re

const bikes = [honda,kawasaki,suzuki]

The main object array can be divied in equal parts based on the number of bikes, like the first few rows with the main object will be "honda", the next few group of rows "Kawasaki" and if more rows added to the bikes that divied the main object further in to equal parts and update

const mainObject =[ 
    { name:"john", age:"23"},
    { name:"Max", age:"26"},
    { name:"Tim", age:"28"},
    { name:"Jim", age:"28"},
    { name:"Jacob", age:"28"},
    { name:"Luke", age:"28"}
]

The expected Outcome should be

const mainObject =[ 
    { name:"john", age:"23","honda"},
    { name:"Max", age:"26","honda"},
    { name:"Tim", age:"28","kawasaki"},
    { name:"Jim", age:"28","kawasaki"},
    { name:"Jacob", age:"28","suzuki"},
    { name:"Luke", age:"28", "suzuki"}
]

CodePudding user response:

Something like this should do the trick:

result = mainObject.map((o, i) => ({
    ...o,
    bike: bikes[Math.floor(i * bikes.length / mainObject.length)]
}))

CodePudding user response:

So Mine is quick long as my JS is also not the best but I wanted to give it a shot

{ name:"john", age:"23","honda"} is not a valid object as they need to be connected by key:value so for the object I made it { name:"john", age:"23", bike: "honda"}

I've also left my console.log()'s in as to try express my thought process.

i simply divided the mainObject by the bikes to get a round number to use the modulus operator % to get no remainder. This however will give an uneven distributuion of bikes or an undefined bike if the mainObject was set to an odd number.

i used a basic forloop to loop through the mainObject and use the modulus operator to do the necessary logic

for (let i = 0, bikeIndex = 0; i < mainObject.length; i  ) {
    mainObject[i] = {
        name: mainObject[i].name,
        age: mainObject[i].age,
        bike: bikes[bikeIndex]
    }
    if ((i   1) % increase_number === 0) {
        bikeIndex  
    }

}

And this should do the trick

Here is the full code with the console.log()'s:

const bikes = ["honda", "Kawasaki", "suzuki"]

const mainObject =[ 
    { name:"john", age:"23"},
    { name:"Max", age:"26"},
    { name:"Tim", age:"28"},
    { name:"Jim", age:"28"},
    { name:"Jacob", age:"28"},
    { name:"Luke", age:"28"}
]

let increase_number = (mainObject.length / bikes.length).toFixed(0)
console.log(increase_number)

console.log(8 % 2)

for (let i = 0, bikeIndex = 0; i < mainObject.length; i  ) {
    console.log(`inc<${increase_number}> | i<${i}> | i   1 = <${i   1}> | Modulus-Logic<${(i   1) % increase_number}>`)
    mainObject[i] = {
        name: mainObject[i].name,
        age: mainObject[i].age,
        bike: bikes[bikeIndex]
    }
    if ((i   1) % increase_number === 0) {
        bikeIndex  
    }

}

mainObject.forEach(object => {
    console.log(object)
})
  • Related