-2

I have an object set. I want to get the value in position 0 . Example. men is at position zero. i want to access label and innerlabel inside options .

const obj1 = {
    men: {
      value: "men",
      attribute_label: "Men",
      type: "select",
     options: {
        1: { innerlabel: "infant", value_string: "1" },
        2: { innerlabel: "baby", value_string: "2" }
      }
    },
    women: {
      value: "women",
      attribute_label: "Women",
      type: "select",
      options: {
        1: { innerlabel: "infant", value_string: "1" },
        2: { innerlabel: "baby", value_string: "2" }
      }
    },
    kids: {
      value: "kids",
      attribute_label: "Kids",
      type: "select",
     options: {
        7: { innerlabel: "infant", value_string: "1" },
        8: { innerlabel: "baby", value_string: "2" }
      }
    }
  };
Rspp
  • 53
  • 7
  • So you want to return the men Object inside set1? – TEK Jun 25 '20 at 13:50
  • 1
    `fisrtValue = Object.values(set1)[0]` – gorak Jun 25 '20 at 13:50
  • There is no *position 0* in objects. I mean, you can use `Object.keys(obj1)[0]` and other static methods but there is no guarantee that it will be `men`. Objects are not meant to be used as an ordered collection – adiga Jun 25 '20 at 13:56
  • ok . so i there anyway i can order it ? @adiga – Rspp Jun 25 '20 at 14:24

2 Answers2

1

const getObjAt = (data, pos) => {
  return Object.values(data)[pos];
};

const obj1 = {
  men: {
    value: 'men',
    attribute_label: 'Men',
    type: 'select',
    options: {
      1: { innerlabel: 'Men', value_string: '1' },
      2: { innerlabel: 'Boy', value_string: '2' },
    },
  },
  women: {
    value: 'women',
    attribute_label: 'Women',
    type: 'select',
    options: {
      1: { innerlabel: 'women', value_string: '1' },
      2: { innerlabel: 'lady', value_string: '2' },
    },
  },
  kids: {
    value: 'kids',
    attribute_label: 'Kids',
    type: 'select',
    options: {
      7: { innerlabel: 'infant', value_string: '1' },
      8: { innerlabel: 'baby', value_string: '2' },
    },
  },
};

const objAt_0 = getObjAt(obj1, 0);
console.log(objAt_0.attribute_label);
Object.values(objAt_0.options).map(option => console.log(option.innerlabel));

const objAt_1 = getObjAt(obj1, 1);
console.log(objAt_1.attribute_label);
Object.values(objAt_1.options).map(option => console.log(option.innerlabel));
Nithish
  • 5,393
  • 2
  • 9
  • 24
0

You can take Object.values() of the object and get its first position:

var set1 = {
  men: { value: "men", label: "Men", type: "select", "options": [ {innerlabel: "boy", value_string: "1"}, {label: "Guy", value_string: "2"}] },
  women: { value: "women", label: "Women", type: "select", "options": [ {innerlabel: "lady", value_string: "1"} , {label: "girl", value_string: "2"}] },
  kids: { value: "kids", label: "Kids", type: "select" ,"options": [ {innerlabel: "infant", value_string: "1"} , {innerlabel: "baby", value_string: "2"}] },
};

const [firstValue] = Object.values(set1);

console.log(firstValue);
gorak
  • 5,233
  • 1
  • 7
  • 19