how can i transform this object in to array?

simon blanco

Hi I have this object and I want to loop over this object with a forEach but I can't, how can I transform it into an array? Sorry for my ignorance but is this a json object?

{
  file0: {
    fieldName: 'file0',
    originalFilename: '4.png',
  },
  file1: {
    fieldName: 'file1',
    originalFilename: '1.jpg',
  },
  file2: {
    fieldName: 'file2',
    originalFilename: '2.jpg',
  },
  file3: {
    fieldName: 'file3',
    originalFilename: '3.png',
  }
}

The array would be something like this:

[
{},
{},
{},
{}
]
mplungjan

Which one of these do you need?

The one with just the objects or one with objects AND key?

const obj = {
  file0: {
    fieldName: 'file0',
    originalFilename: '4.png',
  },
  file1: {
    fieldName: 'file1',
    originalFilename: '1.jpg',
  },
  file2: {
    fieldName: 'file2',
    originalFilename: '2.jpg',
  },
  file3: {
    fieldName: 'file3',
    originalFilename: '3.png',
  }
}

// fast
const arr1 = Object.values(obj).slice(0); // see below why I slice
console.log(arr1);

// alternative using map: 

const arr2 = Object.keys(obj).map(key => obj[key]);
console.log(arr2);

// include the key:

const arr3 = Object.keys(obj).map(key => ({ [key]: obj[key] }));
console.log(arr3);

// Taking the Object.values directly, will POINT to the original object:
const arr4 = Object.values(obj);
arr4[0].fieldName = "XXX"
console.log(obj);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I transform the next object array?

How can I transform an variable, or the record, into an array

how to transform array of c# primitive type assigned to Object into something I can iterate through?

How can I transform a JSON array returned by jq into an object with a single attribute with that list's data?

How can I Transform object in objects to array with es6 or lodash

How to transform an array to an object

How to transform Array to Object?

How can I transform / cast a PHP parent object to a child object?

How can I transform an object to be grouped by object keys

How can I transform a SF object into a Spatial Points Data Frame?

How can I transform this object with strings into a normal one?

How can I map an Array or an Object to an Object?

How can I turn an array object into an object?

How to transform an object into an array of objects?

How to transform object into sorted array?

How can I transform my array to 0 or 1 elements?

How can I use a dictionary to transform an array in python?

How can I get table tuple and transform it into an array in C for postgres?

How can I transform a multidimensional array to a simpler structure?

Numpy - How can I transform this array without using python loops?

How to transform an array of object as array into array

Transform an object to a list[object] in Realm as migration ( How can i replace a list[objects] with object field in Realm? )

How do I transform an array?

How can I transform this dataframe?

How can i transform this table?

How to transform array of object into more advanced array

how can i change array of object to object of array in javascript?

how can I filter array in object?

How can I pass the array object?