1

I know to convert array to object, we use

var object = Object.assign({}, array);

I get an array from multiple select input. like this

var patterns = array["pattern1", "pattern2", "pattern3"];

Then after using object.assign I get object like this.

patterns = {
  0: pattern1,
  1: pattern2,
  2: pattern3
}

I want to add more properties and values to the object values like this:

   patterns = {
   0:{
      name: pattern1,
      status: 0
   },
   1:{
      name: pattern2,
      status: 0
      }
    }

How can I achieve this?

4
  • What is array["pattern1", "pattern2", "pattern3"];? Commented May 7, 2020 at 15:56
  • Patterns is an array made of values received from select [ type = 'multiple' ]. Commented May 7, 2020 at 16:01
  • patterns.map( i => { name: i, status: 0 } ) Commented May 7, 2020 at 16:06
  • Error: unexpected token ':' Commented May 7, 2020 at 16:08

1 Answer 1

1

Maybe like this:

var patterns = ["pattern1", "pattern2", "pattern3"];

function parrern_to_objarr(_patterns){
	var out = [];
	for(var key in _patterns){
		var out_obj = {};
		out_obj.name = _patterns[key];
		out_obj.status = 0;
		/* more properties if need... */
		out.push(out_obj);
	}
	return out;
}

console.log(parrern_to_objarr(patterns));

Sign up to request clarification or add additional context in comments.

1 Comment

@Mohit Sachdeva Thanks!

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.