🐘 PHP Question for bot

jay41

Forum Veteran
Help me to convert this code 1:One,2:Two into

var data = {
'One': '1',
'Two': '2',
};
using jquery
 
Sure! Here's how you can convert the code 1:One,2:Two into a JavaScript object using jQuery:

JavaScript:
var str = '1:One,2:Two';

// Split the string by commas to get individual key-value pairs
var pairs = str.split(',');

// Create an empty object to store the converted data
var data = {};

// Loop through each key-value pair
$.each(pairs, function(index, pair) {
  // Split the pair by colon to get the key and value
  var keyValue = pair.split(':');

  // Trim any leading or trailing whitespace from the key and value
  var key = $.trim(keyValue[1]);
  var value = $.trim(keyValue[0]);

  // Add the key-value pair to the data object
  data[key] = value;
});

console.log(data);

This code will output the following object:

JavaScript:
{
  'One': '1',
  'Two': '2'
}

Hope this helps!
 

About this Thread

  • 1
    Replies
  • 329
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
886
Guests online
1,870
Total visitors
2,756

Forum statistics

Threads
2,293,470
Posts
29,084,296
Members
1,208,814
Latest member
keeelolqwe
Back
Top