简体   繁体   中英

Extract the values from a specific key in a json file

I have a data which is this(I extract this from a json file):

    {
      data1:[
        "Hello",
       "How are you",
         "Are u alright"
         ],
      data2:[ "Hello",
      "How are you",
      "Are u alright",
      "HOwdy!"
      ]
   }

There is this thing that i need either the data1 values or data2 values ie, array as a whole based on the input,,if i enter the input as data1 then data1 array should be stored in another value and vice versa.I tried using Object.keys() and filter functions and can't get the array as a whole.

Input:

         data1

Output:

       [
        "Hello",
       "How are you",
         "Are u alright"
         ]

You can get the values using brackets notation like

 const obj = { data1:[ "Hello", "How are you", "Are u alright" ], data2:[ "Hello", "How are you", "Are u alright", "HOwdy!" ] } const input1 = 'data1'; const input2 = 'data1'; console.log(obj[input1], obj[input2]); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM