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>'
            );
    });
5

4 Answers 4

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>'
  );
});

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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>)

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.