0

I need to build a map "A" from an existing array of objects. However the key value pairs on Map A are from the values of existing Object keys "id" and "cap".

Is it possible to read the values of 2 keys and store as an object

  var items =  [{
                "id": 1,
                "name": "Primary",
                "cap": [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }]
                },{
                "id": 2,
                "name": "Secondary",
                "cap": [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }
                ]
            }]

My map needs to be like this

          { "1" : [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }],
              "2" : [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }]
 }

3 Answers 3

2

Use Array#reduce to achieve the results like below:

var items = [{
  "id": 1,
  "name": "Primary",
  "cap": [{
    "id": "1",
    "name": "1s"
  }, {
    "id": "2",
    "name": "T2s"
  }]
}, {
  "id": 2,
  "name": "Secondary",
  "cap": [{
    "id": "1",
    "name": "1s"
  }, {
    "id": "2",
    "name": "T2s"
  }]
}];

var ans = items.reduce(function(v, i) {
  v[i.id] = i.cap;
  return v;
}, {});

console.log(ans);

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this using a simple loop on the original array, and defining a new key: value pair into the object.

// Create the map
var map = {}
// For every 'item' within the 'items' array
items.forEach(item => {
    // Map the item ID to the item.cap array
    map[item.id] = item.cap
}

var items = [{
  "id": 1,
  "name": "Primary",
  "cap": [{
    "id": "1",
    "name": "1s"
  }, {
    "id": "2",
    "name": "T2s"
  }]
}, {
  "id": 2,
  "name": "Secondary",
  "cap": [{
    "id": "1",
    "name": "1s"
  }, {
    "id": "2",
    "name": "T2s"
  }]
}]

var map = {}
items.forEach(item => {
  map[item.id] = item.cap
})

console.log(map)

Comments

0

Using ES6 Array.from() method.

 var items =  [{
                "id": 1,
                "name": "Primary",
                "cap": [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }]
                },{
                "id": 2,
                "name": "Secondary",
                "cap": [{
                        "id": "1",
                        "name": "1s"
                    }, {
                        "id": "2",
                        "name": "T2s"
                    }
                ]
            }];

var obj = {};
var res = Array.from(items, x => obj[x.id] = x.cap);

console.log(obj);

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.