简体   繁体   English

向对象响应添加键值

[英]add a key value to object response

  $.post("./bigData.php", {'username': username}, function(response) {

            console.log(response);
            var bigUser = response[username]; // attempt one
            var bigUser2 = username.response; // attempt two

       });

My above response consoles like below:我上面的response控制台如下所示:

{"userSumData":[{"id":"slz1","checked":false},{"id":"slz2","checked":false},{"id":"slz3","checked":false},{"id":"slz4","checked":false}, ....."Map":true}

My desired result is this:我想要的结果是这样的:

{"steven":{"userSumData":[{"id":"slz1","checked":false},{"id":"slz2","checked":false},{"id":"slz3","checked":false},{"id":"slz4","checked":false}, ....."Map":true}}

I have tried dot notation and square bracket after my response , all just wipe the data out.我在response后尝试了点表示法和方括号,所有这些都只是将数据擦除。

I think it's rather simple if I get it right.如果我做对了,我认为这相当简单。 Try this ... notation.试试这个...符号。 If it isn't what you want, please comment it out :)如果这不是你想要的,请注释掉:)

 const obj = {"userSumData":[{"id":"slz1","checked":false},{"id":"slz2","checked":false},{"id":"slz3","checked":false},{"id":"slz4","checked":false}],"Map":true}; const name = "steven" const newObj = {[name]: {...obj}} console.log(newObj)

First of all i think that you have JSON format of your data, to convert it into Object you have to use JSON.parse(response) ;首先,我认为您的数据具有 JSON 格式,要将其转换为 Object,您必须使用JSON.parse(response)

You are trying to mutate your response object, don't do it, instead create a new object with the same properties.您正在尝试改变您的响应对象,不要这样做,而是创建一个具有相同属性的新对象。 If i correctly understand your question, you can create a new object using如果我正确理解您的问题,您可以使用创建一个新对象

ES6: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax ES6: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

    let my_object = { inside_object : { steven: { ...response  } }}

ES5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ES5: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

    let my_object = { inside_object : { steven: Object.assign({}, response) }}

You are just assigning to a variable, not an Object property.您只是分配给一个变量,而不是一个 Object 属性。 You need to create an Object, then assign your response to a nameVar property of that Object, like:您需要创建一个对象,然后将您的响应分配给该对象的nameVar属性,例如:

$.post('bigData.php', {username: username}, response=>{
  const obj = {[nameVar]:response}; // notice that we are creating an object and assigning it a variable called obj which has a steven property
  console.log(obj[nameVar]);
}, 'json');

You should, of course, understand asynchronous activity.当然,您应该了解异步活动。 In other words, if you want to use that obj it must be used with the function body that contains your response .换句话说,如果您想使用该obj它必须与包含您的response的函数体一起使用。 It will not be available outside that.在此之外它将无法使用。 You can always create a Promise structure, but that is really just a syntactical indention reducing implementation of the same thing.你总是可以创建一个Promise结构,但这实际上只是一个减少相同事物实现的语法缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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