2

My JSON structure is as follows

var data = [
        {name:'bracket', start_45641654:'46513489431',end_26441:'75434'},
        { name: 'notation', end_746413: '2146464', start_51345641: '76542464' },
    ];

I want to print start, end object values, Here a random number is appending to keys start_ and end_. Tried to use ^ regular expression pattern but it is not working. Is there any other way to print the values?

data.forEach(function (v, i) {
        $('tr').prepend('<td>Name:' + v['name'] + '</td>' +
            '<td>Start:' + v['^start_'] + '</td>' +
            '<td>End:' + v['^end_'] + '</td>'
            );
    });
sridharnetha
  • 2,104
  • 8
  • 35
  • 69
  • Why is your JSON structured like that? – Barmar Jun 14 '21 at 05:18
  • My JSON is not strongly typed. It is generating dynamically. – sridharnetha Jun 14 '21 at 05:20
  • That's no excuse for adding random numbers to the properties. It makes it difficult to process the data in any language. – Barmar Jun 14 '21 at 05:23
  • I agree! But in my case program should be like that structure only :-) – sridharnetha Jun 14 '21 at 05:26
  • [JavaScript: How can I get all the keys and values of an object that begin with a specific string?](https://stackoverflow.com/q/20485270) and [JS getting value of object with key starting with a string](https://stackoverflow.com/q/35279319) and [select all object keys that start with an underscore( _ )](https://stackoverflow.com/q/56160960) – adiga Jun 14 '21 at 06:20

4 Answers4

3

You can't use regular expressions there.

You can loop through the properties of the object, checking if the name has the desired prefix.

data.forEach(function(v) {
  let start, end;
  Object.entries(v).forEach(([key, val]) => {
    if (key.startsWith('start_')) {
      start = val;
    } else if (key.startsWith('end_')) {
      end = val;
    }
  });
  $('tr').prepend('<td>Name:' + v.name + '</td>' +
    '<td>Start:' + start + '</td>' +
    '<td>End:' + end + '</td>'
  );
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

var data = [
        {name:'bracket', start_45641654:'46513489431',end_26441:'75434'},
        { name: 'notation', end_746413: '2146464', start_51345641: '76542464' },
    ];
    
    data.forEach(function (v, i) {
         
         var start = Object.keys(v).find((name) => /start/.test(name));
           var end = Object.keys(v).find((name) => /end/.test(name));
           console.log(start+" "+end);
         
        $('tr').prepend('<td>Name:' + v['name'] + '</td>' +
            '<td>Start:' + v[start] + '</td>' +
            '<td>End:' + v[end] + '</td>'
            );
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr></tr>
</table>

Check this. I don't know if accessing object keys using regex and indexing works. But this should work for you.

anand shukla
  • 666
  • 5
  • 14
0

Note: I write it on the fly you may need to tweak it

for(let item of data) { // iterate through array
  for(let key in item) { // iterate through object keys
    if(!key.hasOwnProperty()) // you may wont need it, it makes sure the key you are bringing aren't inherited and the current object is owner of them, mostly used with arrays
      return
    if(key.indexof('start_') >= 0) // which should be 0 if is ok and -1 if not ok
    {
      // your code for start
      // to get value: item[key]
    }
    if(key.indexof('end_') >= 0) // which should be 0 if is ok and -1 if not ok
    {
      // your code for end
      // to get value: item[key]
    } 
  }
}

Note: that you may read your JSON file/resource as string, not an object (depend on method used), in that case use JSON.parse(<yourStringJson>)

Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
0

You can achieve this using reduce function of array. You don't need to use for, for each loop and condition related logic.

For example:

const data = [
  { name: "bracket", start_45641654: "46513489431", end_26441: "75434" },
  { name: "notation", end_746413: "2146464", start_51345641: "76542464" },
];

console.log(data);

const startEndData = data.reduce((p, c) => {
  const { name, ...rest } = c;
  return [...p, rest];
}, []);

console.log(startEndData);
adiga
  • 34,372
  • 9
  • 61
  • 83
Nirav Vikani
  • 118
  • 8