5

key:[id,name,address]

value:[7,John,NewYork]

I wish to create a json data like{"id": 7, "name": "John", "address": "NewYork"} using for(...){...},

and then return it to ajax

$.ajax({                                
    //what kind of format should json data be here? 
    data:??json data??,
    dataType: 'json',
});

Please help me

4 Answers 4

4

To the first part of your question:

You could use Array#forEach() and assign all properties with the correspondet value.

var key = ['id', 'name', 'address'],
    value = [7, 'John', 'New York'],
    object = {};

key.forEach(function (k, i) {
    object[k] = value[i];
})

document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');

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

2 Comments

You could also use Array#reduce when creating object in order to avoid the standalone forEach. jsfiddle.net/5qhyp9eL/1
Thank you very much,I've solved my problem according to your suggestion.
2

You could:

  • iterate through both arrays...

  • ...add the 1st array element to an empty obj as a key...

  • ...and assign the 2nd array element to the 1st array element as it's value.

  • Each iteration will be paired as key/value when stringified as a JSON.

I'm surprised it actually worked, take a look at the demo below:

SNIPPET

var x = ['id', 'name', 'address'];

var y = [7, 'John', 'NewYork'];

function arrMerge(k, v) {

  var obj = {};

  for (var i = 0; i < k.length; i++) {

    obj[k[i]] = v[i];
  }

  return obj;
}

var z = arrMerge(x, y);

var json = JSON.stringify(z);

console.log(json);

2 Comments

Thanks for helping,$.ajax({...data:json,...}),it returns data successfully.
You're welcome, sir. I was about to do the AJAX part, but I see you have got that part handled.
1

You can write a general function that zips two arrays together into an object if they're of equal length (also assuming they're in the correct order).

function zip(arr1, arr2) {
  if (arr1.length !== arr2.length) return false;
  const obj = {};
  for (let i = 0; i < arr1.length; i++) {
    const key = arr1[i];
    obj[key] = arr2[i];
  }
  return obj;
}

zip(arr1, arr2); // { id: 7, name: "John", address: "NewYork" }

DEMO

You can then use that object as the value of data in your AJAX process.

data

Type: PlainObject or String or Array

Data to be sent to the server. It is converted to a query string, if not already a string.

jQuery documentation

1 Comment

According to your demo,I am able to understand the construting process of obj step by step.
0

I guess unlike forEach, since it returns the result, Array.prototype.reduce() comes very handy for these operations. You can even use it at the stage of declaring your JSON data (jData here).

var   key = ['id', 'name', 'address'],
    value = [7, 'John', 'New York'],
    jData = JSON.stringify(key.reduce((p,c,i) => {p[c] = value[i]; return p},{}));
document.write("<pre>" + jData + "</pre>");

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.