1

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.

9
  • do you have an exact result, you would like to get and your try? Commented Feb 24, 2020 at 21:24
  • Essentially, I'm trying to extract the data in the "text.spaces" field into its own array and at the same time identify objects that do not have this data. Commented Feb 24, 2020 at 21:28
  • please add the wanted result of the given data. Commented Feb 24, 2020 at 21:29
  • 1
    The syntax you're using in the output example doesn't make sense. Commented Feb 24, 2020 at 21:29
  • 1
    Asked and answered, dozens, if not hundreds, of times. Have you googled? It will probably lead you back to an answer right here on Stackoverflow Commented Feb 24, 2020 at 21:29

4 Answers 4

3

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.

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

Comments

1

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);

Comments

0

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

1 Comment

Not exactly. I only want to return the value of the spaces property. For those objects that do not have a spaces property, I want to return a blank space ' '.
0

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);

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.