How to add new object into an arrays after map and check condition

Viewed 178

How can I add new object if value exist

const data = [
          { id: '30', mom: 'A', code: '0141', 'child':' Not used', 'status' :' X' },
          { id: '31', mom: 'B', code: '0141', 'child':' add Child', 'status' :' Y' }
          { id: '32', mom: 'B', code: '0141', 'child':' add child 2', 'status' :' Y' }
        ]

first I map data and if status = X I push new array if not I want to add new object key call child in to array that've status = X

 data.map((element, i) => {
  if (element.status == 'X') {
      arr.push({
          id: element.id,
          mom: element.mom,
          code: element.code
      });
  } else {
      // I want to add element child into arr that has status = X
      arr.push({ child: element.child });
  }
})

I want output like this

const arr = [
  { 
    id: '30', mom: 'A', code: '0141', 
    child: [
      'name':'add child',
      'name':'add child2'
    ]
  },
]
6 Answers
const data = [
          { id: '30', mom: 'A', code: '0141', 'child':' Not used', 'status' : 'X' },
          { id: '31', mom: 'B', code: '0141', 'child':' add Child', 'status' :'Y' },
          { id: '32', mom: 'B', code: '0141', 'child':' add child 2', 'status' : 'Y' }
        ]

Please be careful about your array whitespace.

According to your array: 'status': ' X'

But your 'if structure' looked 'status': 'X'.

You can use my array. And check it.

enter image description here

Unfortunately, your code and your question is a little wrong and hard to understand. Array with key:value, status written with space. So... Sorry if understood it correctly.

This is what I understood

  • Create a new array with all elements with status X and put all child of elements with status Y in that array by the key child.

    const data = [
        { id: '30', mom: 'A', code: '0141', child: ' Not used', status: 'X' },
        { id: '31', mom: 'B', code: '0141', child: ' add Child', status: 'Y' },
        { id: '32', mom: 'B', code: '0141', child: ' add child 2', status: 'Y' }
    ];
    
    let objectX = data.filter(val => val.status == "X");
    objectX["child"] = data.filter(val => val.status == "Y").map(c => c.child);
    /*
    [
      { id: '30', mom: 'A', code: '0141', child: ' Not used', status: 'X' },
      child: [ ' add Child', ' add child 2' ]
    ]
    */
    console.log(objectX);
    

const data = [
          { id: '30', mom: 'A', code: '0141', 'child':'Not used', 'status' :'X' },
          { id: '31', mom: 'B', code: '0141', 'child':'add Child', 'status' :'Y' },
          { id: '32', mom: 'B', code: '0141', 'child':'add child 2', 'status' :'Y' }
        ]
const arr =[]

 data.map((element, i) => {
  if (element.status == 'X') {
      arr.push({
          id: element.id,
          mom: element.mom,
          code: element.code
      });
  }
 else {
      if(!arr[0].child) arr[0].child = []
      arr[0].child.push({'name': element.child});
  }
})

console.log(arr)

Don't mix mutation and transformation. Take Array.push out of your programming vocab and your life will become easier.

This code produces what you want. It won't handle more than the case that you showed, but that's what you asked for.

Your code has syntax errors in it, which made this harder to do than it needed to be. Make sure your code runs and is properly formatted. That will fix 35% of the issues that you run in to as a professional developer.

const data = [
  { id: '30', mom: 'A', code: '0141', 'child':' Not used', 'status' :' X' },
  { id: '31', mom: 'B', code: '0141', 'child':' add Child', 'status' :' Y' },
  { id: '32', mom: 'B', code: '0141', 'child':' add child 2', 'status' :' Y' }
]

const parents = data.filter(e => e.child === ' Not used')
const children = data.filter(c => c.child !== ' Not used')
  .map(c => ({name: c.child}))
const output = parents.map(p => ({...p, child: children}))
console.log(output)

For any NON X statuses

data.map(({child, ...element}) => {
  if (element.status === 'X') {
    arr.push({...element, child: []});
  } else {
    const temp = arr.find((item) => item.status === 'X');
    if (temp) {
      temp.child.push(element)
    }
  }
});

IMHO, more readable)

data.filter((item) => item.status === 'X').map((item) => {
    return item = {
        ...item,
        child: data.filter((item) => item.status !== 'X').map((item) => item.child).map((item) => {
            return {
                ['name'] : item,
            }
        }),
    };
})
Related