0

so i have an array .. with many objects, .. but i want to modify what is inside each object.. but not sure how to go about it. before

[{ filePath: 'stuff/stuff/someplace/',
    path: '/events/places/and/things',
    info:
     { layout: 'event-single',
       permalink: '/events/enigma-2018/',
       title: 'Enigma',
       location: 'Santa Clara, CA',
       description: 'a discription',
       start: 2018-01-30T00:00:00.000Z,
       end: 2018-02-01T00:00:00.000Z,
       address: '101 Great American Pkwy, Santa Clara, CA',
     } 
 }]

desired after

[
     { 
       permalink: '/events/enigma-2018/',
       title: 'Enigma',
       location: 'Santa Clara, CA',
       description: 'a discription',
       start: 2018-01-30T00:00:00.000Z,
       end: 2018-02-01T00:00:00.000Z,
       address: '101 Great American Pkwy, Santa Clara, CA',
     } 
 ]

this array only has one object for this example, but it would be many objects i would be modifying in the same way ..(edited) i think .map should be used.. but thats all i know

2
  • Please paste some what you have tried. Commented Nov 29, 2017 at 18:46
  • Have you read on MDN what Array.prototype.map does? You can probably solve your problem by loosely copy pasting the examples on MDN Commented Nov 29, 2017 at 18:46

6 Answers 6

2
arr.map(function(obj) {
    return {
       permalink: obj.info.permalink,
       title: obj.info.title,
       location: obj.info.title,
       description: obj.info.descriptioin,
       start: obj.info.start,
       end: obj.info.end,
       address: obj.info.address,
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

var myArray = [{ filePath: 'stuff/stuff/someplace/',
    path: '/events/places/and/things',
    info:
     { layout: 'event-single',
       permalink: '/events/enigma-2018/',
       title: 'Enigma',
       location: 'Santa Clara, CA',
       description: 'a discription',
       start: '2018-01-30T00:00:00.000Z',
       end: '2018-02-01T00:00:00.000Z',
       address: '101 Great American Pkwy, Santa Clara, CA',
     } 
 }]
 
 console.log(myArray.map(function(arrayItem) {
     delete arrayItem.info.layout;
     return arrayItem.info
 }));

Comments

0

const myArray = [{
  filePath: 'stuff/stuff/someplace/',
  path: '/events/places/and/things',
  info: {
    layout: 'event-single',
    permalink: '/events/enigma-2018/',
    title: 'Enigma',
    location: 'Santa Clara, CA',
    description: 'a discription',
    start: "2018-01-30T00:00:00.000Z",
    end: "2018-02-01T00:00:00.000Z",
    address: '101 Great American Pkwy, Santa Clara, CA'
  } 
}];

// Do it manually:
const mapped = myArray.map(x => ({
  permalink: x.info.permalink,
  title: x.info.title,
  location: x.info.location,
  description: x.info.description,
  start: x.info.start,
  end: x.info.end,
  address: x.info.address
}));

// Or define a set of keep keys and discard the rest:
const keepKeys = [
  "permalink",
  "title",
  "location",
  "description",
  "start",
  "end",
  "address"
];

const otherMapped = myArray.map(x => Object.keys(x.info).reduce((newObj, key) => {
  if (keepKeys.includes(key)) newObj[key] = x.info[key];
  return newObj;
}, {}));

console.log(mapped);
console.log(otherMapped);

Comments

0

You can use delete command to remove info.layout

var a = [{ filePath: 'stuff/stuff/someplace/',
    path: '/events/places/and/things',
    info:
     { layout: 'event-single',
       permalink: '/events/enigma-2018/',
       title: 'Enigma',
       location: 'Santa Clara, CA',
       description: 'a discription',
       start: '2018-01-30T00:00:00.000Z',
       end: '2018-02-01T00:00:00.000Z',
       address: '101 Great American Pkwy, Santa Clara, CA',
     } 
 }]
 
 
a = a.map(o => {
 
   var o = o.info;
   delete o['layout'];
   return o;

});

console.log(a);

Comments

0

Here, it is a generic way to have a new array that contains only the properties of the nested object. Using typeof, you check if the value of the property is an object and Object.assign to copy the object properties.

var data = [{ filePath: 'stuff/stuff/someplace/',
    path: '/events/places/and/things',
    info:
     { layout: 'event-single',
       permalink: '/events/enigma-2018/',
       title: 'Enigma',
       location: 'Santa Clara, CA',
       description: 'a discription',
       start: "2018-01-30T00:00:00.000Z",
       end: "2018-02-01T00:00:00.000Z",
       address: '101 Great American Pkwy, Santa Clara, CA',
     } 
 }]

var newArray = [];
data.forEach(element => {
var newObj = {};
Object.keys(element).forEach(key => {
  if(typeof element[key] === "object"){
    Object.assign(newObj, element[key]);
    newArray.push(newObj);
  }
})
})

console.log(newArray)

Comments

0

If/when you're comfortable with ES6 syntax you can do this quite concisely:

const out = arr.map(obj => {
  const { layout, ...rest } = obj.info;
  return { ...obj, info: rest }
});

map over the array of objects, use object destructuring to grab the layout property, and then ...rest (spread syntax) for all the other properties. Then just return the object assigning only the properties gathered in rest to the nested info object.

Here's a demonstration.

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.