Home > Software design >  How can i format array object key using JavaScript
How can i format array object key using JavaScript

Time:09-28

I have the array in a variable

var flaredata = [
   {
      "name":"flare.international.Arctic Council",
      "size":3812,
      "imports":
         "flare.interest.Climate & Environment, flare.interest.Economy,flare.interest.Security, Safety & Defence,flare.interest.Health & Social,flare.interest.Inuit Interests"
      
   }
];

But I want an array in this format in which object key is also in double-quotes using JavaScript how can I format array like this also add [] after import ends and before import start

var flaredata = [
   {
      "name":"flare.international.Arctic Council",
      "size":3812,
      "imports":[
         "flare.interest.Climate & Environment",
         "flare.interest.Economy",
         "flare.interest.Security, Safety & Defence",
         "flare.interest.Health & Social",
         "flare.interest.Inuit Interests"
      ]
   }
];

CodePudding user response:

You can convert the imports childens by .split(",").map(x => x.trim());. And use Array.map to process flaredata.

Updated: Fixed split flare.interest.Security, Safety & Defence by comma.

var flaredata = [{
  "name": "flare.international.Arctic Council",
  "size": 3812,
  "imports": [
    "flare.interest.Climate & Environment, flare.interest.Economy,flare.interest.Security, Safety & Defence,flare.interest.Health & Social,flare.interest.Inuit Interests"
  ]
}];

var result = flaredata.map(item => {
   item.imports = item.imports[0]
   .split("flare.interest.")
   .filter(x => x.length > 0)
   .map(x => (('flare.interest.'   x).trim().replace(/,\s*$/, "")));
   
   return item;
})

console.log(result);

CodePudding user response:

Destructure the imports property from all the rest, then return a new object spreading out the rest parameters, and updating imports by splitting it into a new array.

Note: because your input data isn't standard (either , or , ) we can use a regex in the split to check for both instances.

const flaredata = [{"name":"flare.international.Arctic Council","size":3812,"imports": "flare.interest.Climate & Environment,flare.interest.Economy,flare.interest.Security, Safety & Defence,flare.interest.Health & Social,flare.interest.Inuit Interests"}];

const result = flaredata.map(({ imports, ...rest }) => {
  return {
    ...rest,
    imports: [imports.split(/, ?/)]
  };
});

console.log(result);

CodePudding user response:

Please use split function and concat function.

const flaredata = [
   {
      "name":"flare.international.Arctic Council",
      "size":3812,
      "imports":[
         "flare.interest.Climate & Environment, flare.interest.Economy,flare.interest.Security, Safety & Defence,flare.interest.Health & Social,flare.interest.Inuit Interests"
      ]
   }
];

const result = flaredata.map(val => ({ ...val, "imports": [[]].concat(val["imports"][0].split(',')).concat([[]])}))

console.log(result)

  •  Tags:  
  • php
  • Related