0

This is my initial array:

[ 
   { 
    id: 1,
    name: 'pos',
    code: 'pos123',
    appCode: 22,
   },
  { 
    id: 2,
    name: 'tailor',
    code: 'tailor123',
    appCode: 21,
  } 
]

My desired output:

{
   pos123: {
    id: 1,
    name: 'pos',
    code: 'pos123',
    appCode: 22,
  },
  tailor123: {
    id: 2,
    name: 'tailor',
    code: 'tailor123',
    appCode: 21,
  },
}

I tried doing this with map, but I couldn't figure it out. Should I be using reduce here?

6 Answers 6

3

Your output is not an array - it's an object, so you have to use reduce. You can only use map to turn X number of items in an array into X number of items in another array.

const input=[{id:1,name:'pos',code:'pos123',appCode:22,},{id:2,name:'tailor',code:'tailor123',appCode:21,}]
const output = input.reduce((a, obj) => {
  a[obj.code] = obj;
  return a;
}, {});
console.log(output);

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

4 Comments

Sorry, i forget to put, i also need code object inside main object!
Edited. (Try to make sure you post your actual desired output in questions the first time)
obj.code is redundantly included in the new objects
@KooiInc It was originally excluded, but OP asked for it, see comment above
1

You can use Object.assign mapping your array to expected shape of keys and values. Try:

let array = [ 
   { 
    id: 1,
    name: 'pos',
    code: 'pos123',
    appCode: 22,
   },
  { 
    id: 2,
    name: 'tailor',
    code: 'tailor123',
    appCode: 21,
  } 
];

let output = Object.assign(...array.map(x => ({ [x.code]: x })));

console.log(output);

4 Comments

you are putting appCode, name static, but my json contains more than 20 datas?
@MohamedSameer updated, you can use destructuring here
Sorry, i forget to put, i also need code object inside main object!
@MohamedSameer okay, so it's easier. Please modify your original post
1
var yourArray = [ 
 { 
  id: 1,
  name: 'pos',
  code: 'pos123',
  appCode: 22,
  },
 {  
  id: 2,
  name: 'tailor',
  code: 'tailor123',
  appCode: 21,
 } 
];

//this is what you required
var output =  yourArray.reduce((obj, item) => {
  obj[obj.code] =item;
  return obj;
}, {});

Comments

0

Use array reduce function

var data = [{
    id: 1,
    name: 'pos',
    code: 'pos123',
    appCode: 22,
  },
  {
    id: 2,
    name: 'tailor',
    code: 'tailor123',
    appCode: 21,
  }
]

var modData = data.reduce(function(acc, curr) {
  //acc is the empty array passed as argument
  // if empty array does not have a property pos123 or so 
  // create a key by this name
  if (!acc.hasOwnProperty(curr.code)) {
    // then add property to it
    acc[curr.code] = {
      id: curr.id,
      name: curr.name,
      appCode: curr.appCode
    }
  }

  return acc;

}, {})

console.log(modData)

Comments

0

Using loop you can do this.

const resp = [
   {
    id: 1,
    name: 'pos',
    code: 'pos123',
    appCode: 22,
   },
  {
    id: 2,
    name: 'tailor',
    code: 'tailor123',
    appCode: 21,
  }
]

let res = {};

for (let i = 0; i < resp.length; i++) {
  res[resp[i]['code']] = {
    id: resp[i]['id'],
    name: resp[i]['name'],
    appCode: resp[i]['appCode'],
    code: resp[i]['code']
  }
}

console.log(res);

Comments

0

Seems the other solutions include 'code'-property in the resulting object. This one doesn't

const test = [ 
  { 
   id: 1,
   name: 'pos',
   code: 'pos123',
   appCode: 22,
  }, { 
   id: 2,
   name: 'tailor',
   code: 'tailor123',
   appCode: 21,
  } 
];
const test2 = JSON.parse(JSON.stringify(test));
const result = document.querySelector("#result");

result.textContent = "**No redundancy\n" + JSON.stringify(
  test.reduce( (newObj, el) => 
    (newObj[el.code] = delete el.code && el) && newObj, {} 
  ), null, " ");

// alternative
result.textContent += "\n\n**Alternative\n" + JSON.stringify(
  test2.reduce( (newObj, el) => (newObj[el.code] = el) && newObj, {}
  ), null, " ");
<pre id="result"></pre>

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.