0

From this array:

[{
  "map": {
    "name": "2",
    "y": 2
  }
}, {
  "map": {
    "name": "4",
    "y": 17494
  }
}, {
  "map": {
    "name": "3",
    "y": 2
  }
}, {
  "map": {
    "name": "1",
    "y": 1
  }
}]

I want this data structure:

[{
  "name": "2",
  "y": 2
}, {
  "name": "4",
  "y": 17494
}, {
  "name": "3",
  "y": 2
}, {
  "name": "1",
  "y": 1
}]

How can I do this?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    Note that jQuery is primarily a DOM manipulation tool To achieve what you need here you need plain old Javascript. Also, the data structure you have is an array, and nothing to do with JSON. As such I've re-tagged the question for you. – Rory McCrossan Mar 03 '19 at 14:14
  • 1
    Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – adiga Mar 03 '19 at 14:18

5 Answers5

4

Use JavaScript mapping:

    let json = [{
  "map": {
    "name": "2",
    "y": 2
  }
}, {
  "map": {
    "name": "4",
    "y": 17494
  }
}, {
  "map": {
    "name": "3",
    "y": 2
  }
}, {
  "map": {
    "name": "1",
    "y": 1
  }
}];
let result = json.map(item => item.map);
result.forEach(i => console.log(i));

Above code prints:

{name: "2", y: 2}
{name: "4", y: 17494}
{name: "3", y: 2}
{name: "1", y: 1}

See:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
2

Hi you can do this via pure js by looping through the array and extracting out the required output. Please check the code below.

var arr = [{ "map": { "name": "2", "y": 2 } }, { "map": { "name": "4", "y": 17494 } }, { "map": { "name": "3", "y": 2 } }, { "map": { "name": "1", "y": 1 } }];
var resultArr = arr.map(function(item) {
    return item.map;
})
Shreyas Kumar
  • 164
  • 1
  • 6
  • 3
    **.map** doesnot mutate the original array. Instead it returns new array. You should assign the result to a variable. – Maheer Ali Mar 03 '19 at 14:17
2

Use Array.prototype.reduce()

let input=[{
    "map": {
        "name": "2",
        "y": 2
    }
}, {
    "map": {
        "name": "4",
        "y": 17494
    }
}, {
    "map": {
        "name": "3",
        "y": 2
    }
}, {
    "map": {
        "name": "1",
        "y": 1
    }
}]





let revisedarray=input.reduce((acc,val)=>{
 acc.push(val.map);
 return acc;

},[])

console.log(revisedarray)

For more info about array reduce you can look at-mdn docs

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
  • I think this answer should be improved (since it is the accepted by OP answer), using vague single letter variable names it's not going to help anyone, you should also consider adding link to MDN map, reduce. – Vitaliy Terziev Mar 03 '19 at 14:29
1

ES6 Short syntax, here A is original array of objects and A2 is output array of objects

let A2 = A.map(o=>o.map);
Sumer
  • 2,687
  • 24
  • 24
0
let jsonString = your_jsonString;
var blankArr = [];
let result = jsonString.map(item => item.map);
result.forEach(i => blankArr.push(i));
console.log(blankArr);
  • 2
    I know calling an object "JSON" is a very common mistake but I've never seen it called a JSON *string* when it clearly isn't. Moreover, you never treat it as a string but as an array, which it is. The `.map` followed by `.forEach` to push into a new array is completely redundant - `.map` already makes a copy of the array where each element is run through the mapping function. Running `.forEach` to put each of those objects into another array achieves nothing other than wasting CPU cycles and memory. – VLAZ Mar 03 '19 at 14:46
  • let jsonString = your_jsonString; var blankArr = []; let result = jsonString.map(item => blankArr.push(item.map)); console.log(blankArr); This will work? – KRISHNA KANT SINGH Mar 03 '19 at 15:17