0

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);
};
4
  • The format you show is not valid, as "name": "Apple" is not an array element and [ "name": "Apple", "field": "IT" ] is not a valid array. Did you mean to use { and }? Commented Jul 13, 2021 at 17:36
  • @crashmstr yes sorry Commented Jul 13, 2021 at 17:45
  • So what is the actual relation between organisation object and field object? does the field always followed by organisation name? Commented Jul 13, 2021 at 17:54
  • Each organisation (name) has a field such as IT or retail. There will always be a name object and a field object Commented Jul 13, 2021 at 18:16

3 Answers 3

2
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);
Sign up to request clarification or add additional context in comments.

Comments

1

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' } ]

2 Comments

Yep that output is exactly what I need
@Mark check the answer now, sorry It was not formatted before as I typed on my phone :D
0

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);

Comments

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.