2

I need to understand if when I have a first element from the first object in the first array. The structure is this:
let arr = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]

I've done this (see below), but maybe there is shorter solution

let arr = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]

let newArr = [];
//arr[0] - the first array
arr[0].map((item, index) => {
  //pushing items into empty arr to get the first value
  for (let key in item) {
    newArr.push(item[key]);
  }
  console.log(newArr[0]);
});

I need the output like: - the first value of the firts object of the first array. - then other values.

Ori Drori
  • 145,770
  • 24
  • 170
  • 162
Elina
  • 21
  • 2
  • 1
    `arr[0][0].a` ? – Calvin Nunes Jul 17 '19 at 16:49
  • 2
    First item in array: `arr[0]`. First item in array, first item in that array: `arr[0][0]`. Its `a` property: `arr[0][0].a`. Is that what you're after? I'm a bit confused to be quite honest. – Tyler Roper Jul 17 '19 at 16:49
  • I think this `[{"a": 1, "b": 2}]` is the same of this `{"a": 1, "b": 2}` – GrafiCode Jul 17 '19 at 16:51
  • 2
    @CalvinNunes I think the point is that she doesn't know the first property name, it can vary. – Barmar Jul 17 '19 at 16:51
  • 1
    @GrafiCode Well one is an array and one is an object... Though, I agree that the array in such a case isn't serving a ton of purpose. – Tyler Roper Jul 17 '19 at 16:51
  • Yes, this is what I'm after but the name of the first object property can vary. – Elina Jul 17 '19 at 16:53
  • @Barmar yes, could be the problem, but she says: *"the structure **is** this ..."* – Calvin Nunes Jul 17 '19 at 16:53
  • @CodeManiac - Given that the marked duplicate confirms that the key is a random string, and that it's the only key in the object, the boundaries of that request don't present the same dangers that OP's does in regards to creation order. I'm not sure the answers are entirely adequate. – Tyler Roper Jul 17 '19 at 17:08
  • @TylerRoper is there anyway you can guaranty the retrieval of keys in sequence, they do have sequence [`property order`](https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/), `object.values` and `for ... in` loops in same order, if you still feel duplicate aren't sufficient let me know will reopen – Code Maniac Jul 17 '19 at 17:25
  • @CodeManiac The order is guaranteed, yes, but the guaranteed order isn't always *creation order*. See the second snippet in my answer below. If OP wants the "first property", they're going to be very surprised when the one returned is actually the *last* property. It seems like in the marked duplicate, they're retrieving the *only* key. In this question, OP wants the *first* key. Because one is reliant on order and one is not, in my opinion, there's a big difference :) – Tyler Roper Jul 17 '19 at 17:26
  • @TylerRoper you want me to reopen it ? is there any solution which can guaranty will return first property only 1) [`Valid key`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties), 2) [`Does JavaScript Guarantee Object Property Order?`](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Code Maniac Jul 17 '19 at 17:35
  • 1
    @TylerRoper but object is not suitable data structure when order is important, option should be `Map`, `Array` or anything which holds order – Code Maniac Jul 17 '19 at 17:37
  • @CodeManiac Well that's exactly my point. I think that that information is very important to this question, and the duplicate does not cover it. – Tyler Roper Jul 17 '19 at 17:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196589/discussion-between-code-maniac-and-tyler-roper). – Code Maniac Jul 17 '19 at 17:53
  • @CodeManiac agreed, there is also https://stackoverflow.com/questions/983267/how-to-access-the-first-property-of-an-object-in-javascript – Jonas Wilms Jul 17 '19 at 19:10

3 Answers3

3

If you don't know the name, you can use Object.values().

let arr = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]];
let result = Object.values(arr[0][0])[0];

console.log(result);

HOWEVER, object property order is not always the same as its creation order. Be warned that if your object has positive integer keys, you cannot retrieve its creation order.

let arr = [[{"a": 1, "b": 2, 1: "I'm a positive integer key!"}], [{"c": 3, "d": 4}]];
let result = Object.values(arr[0][0])[0];

console.log(result);
Tyler Roper
  • 20,529
  • 6
  • 30
  • 51
1

You can use Object.keys() to get the keys of an object, then get the first element of this. Then you can use that to access the property.

let arr = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]

let newArr = [];
let firstKey = Object.keys(arr[0][0])[0];
console.log(arr[0][0][firstKey]);
Barmar
  • 596,455
  • 48
  • 393
  • 495
0

You can use flat() and then Object.values() as follows:

const arr = [[{"a": 1, "b": 2}], [{"c": 3, "d": 4}]]

console.log(Object.values((arr.flat()[0]))[0])
zb22
  • 2,390
  • 1
  • 16
  • 28