Using a key value in an object in the same object

Related searches

I am trying to get the value from an array in a object.

So two cases;

First if the key and value is in the same object. Second if the key is being used in another object.

const direction = {
    'N': ['E', 'W', 0],
    'E': ['S', 'N', 0],
    'S': ['W', 'E', 0 * Math.sign(-1)],
    'W': ['N', 'S', 0 * Math.sign(-1)],
    x: this.N[2] // Trying to get The value from 'N' in the array
  }

AND

const value = { x: direction.N[2]} //calling the value from the above object

It would be better to create either a class and set your properties inside of the class or the constructor or create a function to generate new objects. The syntax you're attempting to use is not possible.

class Direction {
  N = ['E', 'W', 0];
  E = ['S', 'N', 0];
  S = ['W', 'E', 0 * Math.sign(-1)];
  W = ['N', 'S', 0 * Math.sign(-1)];
  x = this.N[2];
}

const direction = new Direction();

console.log(direction);
// Direction {N: Array(3), E: Array(3), S: Array(3), W: Array(3), x: 0}

Or:

function createDirection() {
  let direction = {
    'N': ['E', 'W', 0],
    'E': ['S', 'N', 0],
    'S': ['W', 'E', 0 * Math.sign(-1)],
    'W': ['N', 'S', 0 * Math.sign(-1)],
  };

  direction.x = direction.N[2];
  return direction;
}

console.log(createDirection());
// {N: Array(3), E: Array(3), S: Array(3), W: Array(3), x: 0}

Using a class has the advantage of allowing additional functions and computed properties (get property() {} syntax) on your object, which will make your code more readable and extendable.

using key value pair of same object inside itself with 'this' in , The answer depends on what this refers to in each context. In JavaScript, this is bound to whatever object was on the left of the dot (.) when the� Object.values () Method The Object.values () method was introduced in ES8 and it does the opposite of Object.key (). It returns the values of all properties in the object as an array. You can then loop through the values array by using any of the array looping methods.

You cant use object initializer like this. It is logically incorrect.

And also if you want to use the items of 'N' array, you can use like following:

const direction = {
    'N': ['E', 'W', 0],
    'E': ['S', 'N', 0],
    'S': ['W', 'E', 0 * Math.sign(-1)],
    'W': ['N', 'S', 0 * Math.sign(-1)]
  }
  
  console.log(direction)
  console.log(direction['N'][2])

Object.values(), The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a forin loop. object with random key ordering // When using numeric keys, the values� The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

By adding a method i could return the values. Is there an issue doing it like this? why use a class?

const direction = {
    'N': ['E', 'W', 0],
    'E': ['S', 'N', 0],
    'S': ['W', 'E', 0 * Math.sign(-1)],
    'W': ['N', 'S', 0 * Math.sign(-1)],
    x() {return this.W[2] + this.E[2]},
    y() {return this.N[2] + this.S[2]}
  }

Object.entries(), The method returns an array of a given object's own enumerable property names, in the same order as we get with a normal for loop. Example of� In the above program, the Object.keys() method and the length property is used to count the number of keys in an object. The Object.keys() method returns an array of a given object's own enumerable property names i.e. ["name", "age", "hobbies"]. The length property returns the length of the array.

Object Keys, Values, and Entries: JavaScript ES6 Feature Series (Pt , The following example creates an object using object literal syntax. Example: Create You must specify key-value pair in object for properties or methods. Object in JavaScript passes by reference from one function to another. Example: JS� Iterate over the property (key,value) pairs of an object: Iterate over the keys, use each key to retrieve the corresponding value. 0; public Object getAt(Object key) Support the subscript operator for a Map. Accept a payment Charges API.

JavaScript Object, var arr1 = Object.keys(obj); Object.values() The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a forin loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). var arr2 = Object.values(obj); For more please go here

var obj = { key: value1, key: value2} I would like to iterate it and get pars of (key and value1) and (key and value2) if I use simple cycle: for (var i in obj){ console.log(obj[i]) } I got: key value2 key value2. so obj[i] always take last key

Comments
  • If you mark down can you say why? might be simple for you
  • Hi mat thank you. will look over class and constructor again. Thank you
  • keep objects simple? can methods be used inside objects? eg. something() => {}
  • Yes, you can define methods in classes.