Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

javascript - Access inner Object and form Jsx

I have two set of objects

const obj1 = {
    men: {
      value: "men",
      attribute_label: "Men",
      type: "select",
      attribute_options: {
        1: { label: "infant", value_string: "1" },
        2: { label: "baby", value_string: "2" }
      }
    },
    women: {
      value: "women",
      attribute_label: "Women",
      type: "select",
      attribute_options: {
        1: { label: "infant", value_string: "1" },
        2: { label: "baby", value_string: "2" }
      }
    },
    kids: {
      value: "kids",
      attribute_label: "Kids",
      type: "select",
      attribute_options: {
        7: { label: "infant", value_string: "1" },
        8: { label: "baby", value_string: "2" }
      }
    }
  };

and set2

const obj2 = {
men: { code: "men", title: "Men" },
women: { code: "men", title: "Men" }
};

I want to compare both the set of object and if key is same i want to get the result as new object

    const resultobj = {
     men: {
  value: "men",
  attribute_label: "Men",
  type: "select",
  attribute_options: {
    1: { label: "infant", value_string: "1" },
    2: { label: "baby", value_string: "2" }
  }
},
women: {
  value: "women",
  attribute_label: "Women",
  type: "select",
  attribute_options: {
    1: { label: "infant", value_string: "1" },
    2: { label: "baby", value_string: "2" }
  }
}
    };

And i want to display label from result obj and show label inside option . I have label in Men and inside option i have other 2 label boy and guy. Same in the case of Women. So i want to display the value inside option as well. Example format i have mentioned.

Men 

boy Guy

Women

Lady  Girl
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

const set2 = {
  men: { code: "men", title: "Men" },
  women: { code: "Women", title: "Women" },
};



let keys = Object.keys(set1).filter(key => key in set2)

let result = {} 
keys.forEach((key)=>{
      result = {...result,...{[key]:set1[key]}}
})

console.log(result)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...