0

So I would have the next string:

const string = "1,22, 28,40,4, 8,24,31,33"

var config = {
        config_key: config_key,
        location_key: null,
        autoassign: 1,
                    }

I would need to create 9 objects from this string where the location_key: is equal with a value in the string, the other object prop. remains the same.

5
  • Have you tried anything that isn't exactly working how you want? If so, please add it to your question Commented Apr 13, 2020 at 22:58
  • Also, should the numbers from the string remain as strings or should they be interpreted as numbers? Commented Apr 13, 2020 at 23:03
  • I've played around with it, trying to convert it to array first and then trying to create the objects ... but my head is spinning around when it comes to array and objects, and moving data from one to another. Just start to learn. Commented Apr 13, 2020 at 23:10
  • You should edit your question to show those attempts. Also, about those strings vs numbers.... ? Commented Apr 13, 2020 at 23:12
  • no they don't have to be numbers, seems that squlize is inserting them into the database correctly. thanks Commented Apr 13, 2020 at 23:24

2 Answers 2

1

Split the string on /, ?/ (comma and optional space) then map those values to new objects, using config as a template and override the location_key using the string value.

const string = "1,22, 28,40,4, 8,24,31,33"

const config = {
  config_key: 'config_key',
  location_key: null,
  autoassign: 1,
}

const parsed = string.split(/, ?/).map(location_key => ({
  ...config,
  location_key
}))

console.info(parsed)


Since your question states...

where the location_key: is equal with a value in the string

I assumed you wanted to keep the values from the string as strings but if you want them treated as actual numbers (integers), use

location_key: parseInt(location_key, 10)
Sign up to request clarification or add additional context in comments.

Comments

1

I would use a constructor to create each new instance of an Object:

const string = "1,22, 28,40,4, 8,24,31,33", config_key = 'testing';
function Config(locationKey){
  this.config_key = config_key;
  this.location_key = +locationKey;
  this.autoassign = 1;
}
const nums = string.split(/\s*,\s*/), objs = [];
nums.forEach(n=>{
  objs.push(new Config(n));
});
console.log(objs);

3 Comments

thanks for taking the time to answer, the ES6 method seems more simple but yours is nice too.
You did notice those location_keys are numbers in my example. And that is ES6. Notice the arrow function. I prefer constructors to classes, for a number of reasons you've yet to understand.
Yes, I have noticed, I don't really need them numbers but it is a good example to save and have for the future. Thanks again for taking the time.

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.