-1

Nested Array

[ { "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }] 

What I'm trying to achieve

[ { "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "name": "Annotation 2020-04-08 135240.jpg" }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "name": "Annotation 2020-04-08 135240.jpg" }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "name": "Annotation 2020-04-08 135240.jpg" }] 
2

3 Answers 3

4

You can easily just map the file.name property to it's parent and then delete d.file when finished.

Basic Example:

const data = [{ "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } } ]; // [Option 1] Map and return new object const targetData = data.map(d => ({ id: d.id, created_at: d.created_at, name: d.file.name })); // [Option 2] Create new property and delte old one const targetData2 = data.map(d => { d.name = d.file.name; delete d.file; return d; }); console.log(targetData); console.log(targetData2);

You could also use something like Array.reduce or some other method, but that's the basic idea.

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

1 Comment

Better to use destructuring and taking the desired properties instead of deleting them
1

Here you can use map function.

This will create new array. You can assign value to keys.

This will not delete your original array.

const arr = [{ "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } } ] var newArr = arr.map(function(elem) { return { id: elem.id, created_at: elem.created_at, name: elem.file.name } }); console.log(newArr);

Comments

1

Destructure out {file: name} property and use rest operator to put rest of properties in object o.
Merge object o with property name name using spread operator. Uses shorthand properties.

Makes copies of all objects. Does not modify original objects in data.

data = [{ "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } } ]; data = data.map( ({file: {name}, ...o}) => ({...o, name}) ) console.log(data);

fast in-place mutation (modifies objects in-place) using a for-of loop. set name property and delete file property:

data = [{ "id": 16, "created_at": "2020-07-01T14:09:14.066Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 15, "created_at": "2020-07-01T14:08:31.558Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } }, { "id": 14, "created_at": "2020-07-01T14:07:32.869Z", "file": { "name": "Annotation 2020-04-08 135240.jpg" } } ]; for(const x of data) { x.name = x.file.name delete x.file } console.log(data)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.