Home > Mobile >  How to get object inside array and add it to parent object
How to get object inside array and add it to parent object

Time:06-06

I have data like this :

data = {
  data: [
    {
      Email: '[email protected]',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: '[email protected]',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: '[email protected]',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}

I want to get the first array data inside data and add it to parent object.

this is what I expected :

data = {
  Email: '[email protected]',
  name: 'dsadas',
  amount: '43.243.434',
  date: '22 Januari 2022',
  data: [
    {
      Email: '[email protected]',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: '[email protected]',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: '[email protected]',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}

I was trying using map and pushing to object but still no luck

How to solve that ?

Please let me know if you need more information if it's still not enough

CodePudding user response:

You could simply use a spread operator syntax here to individually add all elements of the first array entry to the initial object.

const initialData = {
    data: [
        {
            Email: '[email protected]',
            name: 'dsadas',
            amount: '43.243.434',
            date: '22 Januari 2022',
        },
        {
            Email: '[email protected]',
            name: 'akdasd',
            amount: '3.234',
            date: '12 Februari 2022',
         },
         {
            Email: '[email protected]',
            name: 'dasdasd',
            amount: '234.324',
            date: '10 Februari 2022',
         }
    ]
};

const finalData = { ...initialData.data[0], ...initialData };

CodePudding user response:

If you want to apply the source's properties to target object,
Check Object.assign method.

const yourData = {
  data: [
    {
      Email: '[email protected]',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: '[email protected]',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: '[email protected]',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
};

Object.assign(yourData, yourData.data[0]);

CodePudding user response:

You can use an iteration too :

var obj = {
  data: [
    {
      Email: '[email protected]',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: '[email protected]',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: '[email protected]',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}


Object.getOwnPropertyNames(obj.data[0]).forEach((e) => {
  obj[e] = obj.data[0][e]
})

console.log(obj)

CodePudding user response:

var ab = {
  data: [
    {
      Email: '[email protected]',
      name: 'dsadas',
      amount: '43.243.434',
      date: '22 Januari 2022',
    },
    {
      Email: '[email protected]',
      name: 'akdasd',
      amount: '3.234',
      date: '12 Februari 2022',
    },
    {
      Email: '[email protected]',
      name: 'dasdasd',
      amount: '234.324',
      date: '10 Februari 2022',
    }
  ]
}
var firstIttem = ab.data[0]
var abc = {
    Email: firstIttem.Email,
    name: firstIttem.name,
    amount: firstIttem.amount,
    date: firstIttem.date,
    data: [...ab.data]
}
console.log(abc)

  • Related