Paul
Paul

Reputation: 45

How do I extract an object(array) from an array of objects?

Say I have the following dataset:

const art = {
    'fields': [
        {title:'Title 1'}, 
        {'text': [
            {spaces: '1'}, {link: 'This is a link'}, {mouse: 'Yes'}
        ]}, 
        {title: 'Title 2'}, 
        {title:'Title 3'},
        {'text': [
            {spaces: '2'}, {link: 'This is a different link'}, {mouse: 'No'}
        ]},
    ]};

I would like to extract a new object(array) from the "spaces" property. In addition to that, I need to to identify those "title" objects that do not have a "spaces" property associated with them with a blank value.

I'd like to end up with something like this:

newArray = ['', '1', '', '', '2']

Is this possible? Forgive me if my terminology isn't great. New at this.

Upvotes: 1

Views: 319

Answers (4)

Sunil Lama
Sunil Lama

Reputation: 4539

if your text array will have multiple spaces then it should loop through it and add in spaces value like below:

const art = {
  'fields': [{
      title: 'Title 1'
    },
    {
      'text': [{
        spaces: '1'
      }, {
        link: 'This is a link'
      }, {
        mouse: 'Yes'
      }]
    },
    {
      title: 'Title 2'
    },
    {
      title: 'Title 3'
    },
    {
      'text': [{
        spaces: '2'
      }, {
        link: 'This is a different link'
      }, {
        mouse: 'No'
      }]
    },
  ]
};


var res = art.fields.reduce((ini, curr, idx) => {
  if (curr.text) {
    curr.text.forEach(arr => {
      arr.spaces && ini.push(arr.spaces);
    });
  } else {
    ini.push('');
  }
  return ini;
}, [])
console.log(res);

Upvotes: 0

Guffa
Guffa

Reputation: 700422

You can use the map method to calculate a value for each item in the art.fields array.

const art = {
    'fields': [
        {title:'Title 1'}, 
        {'text': [
            {spaces: '1'}, {link: 'This is a link'}, {mouse: 'Yes'}
        ]}, 
        {title: 'Title 2'}, 
        {title:'Title 3'},
        {'text': [
            {spaces: '2'}, {link: 'This is a different link'}, {mouse: 'No'}
        ]},
    ]};

var newArray = art.fields.map(function(o){
  return 'text' in o ? o.text[0].spaces : '';
});

console.log(newArray);

Upvotes: 1

HoldOffHunger
HoldOffHunger

Reputation: 20891

It shouldn't be too hard. Try Javascript's map() function...

const art = {
    'fields': [
        {title:'Title 1'}, 
        {'text': [
            {spaces: '1'}, {link: 'This is a link'}, {mouse: 'Yes'}
        ]}, 
        {title: 'Title 2'}, 
        {title:'Title 3'},
        {'text': [
            {spaces: '2'}, {link: 'This is a different link'}, {mouse: 'No'}
        ]},
    ]};

const spaces = art.fields.map(function(field) {
  if(field.text) {
    return field.text[0].spaces;
  }
  return '';
});

console.log("Spaces?");

console.log(spaces);

And the results...

"Spaces?"
["", "1", "", "", "2"]

See the code working on JSBIN. Map returns a function's result for an iteration over an array. Check out the Mozilla Developer Network Docs on it.

Upvotes: 3

cristos
cristos

Reputation: 21

you could do something like this

const newArr = art.fields.map(space => {
  return {...space.text}
});

so I'm assuming you want to return a new array containing an object of the text array

Upvotes: 0

Related Questions