react-native : How to format object value properly

25
June 16, 2019, at 12:40 PM

I am trying to create an empty object. First, each field will have a name with empty values. Every thing works fine but I am having a trouble when formatting the name field

This is my code:

@observable member = {
//This name field doesn't work
    [{name:''}] :
    {
      age: '',
      email: '',
      job:'',
      info: [
        {
          email: '',    
          phone:'',
          address: ''
        }
      ],
    }
  }

When I console.log(member) I get this:

[object Object]: {dots: "", selected: "", day: "", task: Array(1), name: ""}

Instead of [object Object] I want to get name:'' Is it possible to do this?

Answer 1

Objects are written as name: value pairs, your object doesn't have a proper key which is [{name: ''}]. You can do the following to properly format your object.

@observable member = {
 // will store names in array
 // or just do 
 // name: '',
 name: '',
 age: '',
 email: '',
 job:'',
 info: [
    {
      email: '',    
      phone:'',
      address: ''
      }
   ],
}

or

@observable member = {
  names : [{'name': ''}],
  memberInfo: {
  age: '',
  email: '',
  job:'',
  info: [
    {
      email: '',    
      phone:'',
      address: ''
    }
  ],
  }
}

Then access the value, like

// if it's the array
member.names[0].name
// or if it's not an array
member.name
Answer 2

You can convert Javascript object to JSON using JSON.Stringify().

console.log(JSON.Stringify(member).name)

Should do the job.

READ ALSO
How can I get the innerHTML of a clicked button [duplicate]

How can I get the innerHTML of a clicked button [duplicate]

This question already has an answer here:

30
Function that is intended to return an array is returning undefined. Console.log(array) within function returns the array

Function that is intended to return an array is returning undefined. Console.log(array) within function returns the array

I am trying to write a function that takes an array and returns a new array with all elements shifted n indices to the leftFor example:

72
Selenium Actions moveToElement on Google Chrome 75.0.3770.80 on ubuntu doesnot work

Selenium Actions moveToElement on Google Chrome 75.0.3770.80 on ubuntu doesnot work

Selected an element and wanted --> physically move the mouse cursor over it

37