Mark
Mark

Reputation: 27

Split array values into new array of keys/values

I have an array of objects like the following:

organisationData = [
    {
            "id": -516538792,
            "label": "name",
            "value": "Apple",
            "key": "name"
        },
        {
            "id": -586565959,
            "label": "field",
            "value": "IT",
            "key": "field"
        },
        {
            "id": 2081552018,
            "label": "name",
            "value": "Microsoft",
            "key": "name"
        },
        {
            "id": -1094152060,
            "label": "field",
            "value": "IT",
            "key": "field"
        }
    ]

I need to split this array so that it follow the following format:

organisations = [
    {
        "name": "Apple",
        "field": "IT"
    },
    {
        "name": "Microsoft",
        "field": "IT"
    }
]

I can create a new array that follows the correct format but each 'company' is part of the same array and I'd like to split them out, how do I modify the following?:

let organisationData = [.....]

let organisations = [];

for (org of organisationsData) {
    _org = [];
    _org[org.key] = org.value;
    organisations.push(_org);
};

Upvotes: 0

Views: 448

Answers (3)

Saif Farooqui
Saif Farooqui

Reputation: 150

const organisationData = [
  {
    id: -516538792,
    label: "name",
    value: "Apple",
    key: "name",
  },
  {
    id: -586565959,
    label: "field",
    value: "IT",
    key: "field",
  },
  {
    id: 2081552018,
    label: "name",
    value: "Microsoft",
    key: "name",
  },
  {
    id: -1094152060,
    label: "field",
    value: "IT",
    key: "field",
  },
  {
    id: 2081552018,
    label: "name",
    value: "Amazon",
    key: "name",
  },
  {
    id: -1094152060,
    label: "field",
    value: "IT",
    key: "field",
  },
  {
    id: 2081552018,
    label: "name",
    value: "Dell",
    key: "name",
  },
  {
    id: -1094152060,
    label: "field",
    value: "Laptop Manufacturing",
    key: "field",
  },
];

let newOrgArray = [];

organisationData.map((m, index) => {
  if (index % 2 === 0) {
    let orgObj = { name: "", feild: "" };
    orgObj["name"] = m.value;
    orgObj["feild"] = organisationData[index + 1].value;
    newOrgArray.push(orgObj);
  }
});

console.log(newOrgArray);

Upvotes: 2

Giant Brain
Giant Brain

Reputation: 124

var organisationData = [
    {
            "id": -516538792,
            "label": "name",
            "value": "Apple",
            "key": "name"
        },
        {
            "id": -586565959,
            "label": "field",
            "value": "IT",
            "key": "field"
        },
        {
            "id": 2081552018,
            "label": "name",
            "value": "Microsoft",
            "key": "name"
        },
        {
            "id": -1094152060,
            "label": "field",
            "value": "IT",
            "key": "field"
        }
    ];

var organisation = [];
var i = 0;
var temp = '';

organisationData.forEach(function(org){
  if(org.key == 'name'){
    temp = org.value;
  }
  if(org.key == 'field'){
    organisation[i] = {'name': temp, 'field': org.value};
    i++;
  }
});

console.log(organisation);

Upvotes: 0

Abhi
Abhi

Reputation: 1005

Is this what you trying to achieve :

Data = [
         {
            "id": -516538792,
            "label": "name",
            "value": "Apple",
            "key": "name"
        },
        {
            "id": -586565959,
            "label": "field",
            "value": "IT",
            "key": "field"
        },
        {
            "id": 2081552018,
            "label": "name",
            "value": "Microsoft",
            "key": "name"
        },
        {
            "id": -1094152060,
            "label": "field",
            "value": "IT",
            "key": "field"
        }
    ];
   result = [] 
   for (var i = 0; i < Data.length; i+=2) {
    
       Temp = {};
       Temp[Data[i]['key']] = Data[i]['value'];
       Temp[Data[i+1]['key']] = Data[i+1]['value'];
    
        result.push(Temp)
  } 
  console.log(result);

Output :

[ { name: 'Apple', field: 'IT' }, { name: 'Microsoft', field: 'IT' } ]

Upvotes: 1

Related Questions