0

I have an array of objects and I want to create a single object where if there is a common key it will create a new array under that key. For example let's say, I have below array:

[{
  "Steering": ["Steering Type : EPAS with Pull Drift Compensation technology"],
  "Power \u0026 Performance": ["1.5L Petrol Ti-VCT MT : ", "1.5L Petrol Ti-VCT AT : ", "1.0L Petrol Ecoboost MT : ", "1.5L Diesel DV5 MT : "],
  "Wheels and Tyres": ["Tyres : 195 / 65 R15", "Wheels in inch : 15\" Steel Wheels"],
}, {
  "INSTRUMENTATION": ["Tachometer : ", "Door Ajar Indicator : ", "Low Fuel Warning Lamp : ", "Distance To Empty (DTE) Display : ", "Headlamp on Key-off warning : "],
  "TYRES \u0026 WHEELS": ["Tyres : 175/65R14", "Wheel Size : 14\" Steel(35.6)cms"],
  "BRAKES": ["Front : Ventilated Discs", "Rear : Self Adjusting Drums "],
}, {
  "INSTRUMENTATION": ["Tachometer : ", "Door Ajar Indicator : ", "Low Fuel Warning Lamp : ", "Distance To Empty (DTE) Display : ", "Headlamp on Key-off warning : "],
  "TYRES \u0026 WHEELS": ["Tyres : 175/65R14", "Wheel Size : 14\" Alloy (35.6)cms"],
  "BRAKES": ["Front : Ventilated Discs", "Rear : Self Adjusting Drums "],
}]

I have repetitive INSTRUMENTATION, BRAKES, TYRES & WHEELS keys.

I want to create a single object where the key would not be repeated and if the same key exists already, it creates a new sub array object. Like under INSTRUMENTATION, BRAKES and TYRES & WHEELS keys, a new array object will be created.

I don't want to use any third party keyword except jQuery.

Expecting result should be :-

[
    {
      "Steering": [
        "Steering Type : EPAS with Pull Drift Compensation technology"
      ],
      "Power & Performance": [
        "1.5L Petrol Ti-VCT MT : ",
        "1.5L Petrol Ti-VCT AT : ",
        "1.0L Petrol Ecoboost MT : ",
        "1.5L Diesel DV5 MT : "
      ],
      "Wheels and Tyres": [
        "Tyres : 195 \/ 65 R15",
        "Wheels in inch : 15\" Steel Wheels"
      ],
      "INSTRUMENTATION": [
        [
          "Tachometer : ",
          "Door Ajar Indicator : ",
          "Low Fuel Warning Lamp : ",
          "Distance To Empty (DTE) Display : ",
          "Headlamp on Key-off warning : "
        ],
        [
          "Tachometer : ",
          "Door Ajar Indicator : ",
          "Low Fuel Warning Lamp : ",
          "Distance To Empty (DTE) Display : ",
          "Headlamp on Key-off warning : "
        ]
      ],
      "TYRES & WHEELS": [
        [
          "Tyres : 175\/65R14",
          "Wheel Size : 14\" Steel(35.6)cms"
        ],
        [
          "Tyres : 175\/65R14",
          "Wheel Size : 14\" Alloy (35.6)cms"
        ]
      ],
      "BRAKES": [
        [
          "Front : Ventilated Discs",
          "Rear : Self Adjusting Drums "
        ],
        [
          "Front : Ventilated Discs",
          "Rear : Self Adjusting Drums "
        ]
      ]
    }
]
2
  • Welcome to SO! Please refer How to ask and provide necessary details Commented Nov 1, 2016 at 7:51
  • Can you explain a little more about the format you want to achieve - at the moment your question makes very little sense. An example of the output may help. Commented Nov 1, 2016 at 7:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.