[英]JSON - How do I add object in an objectArray string?
I'm kinda new to javascript.我对 javascript 有点陌生。 Few days ago I started a node js project.
几天前我开始了一个node js项目。 I had some javascript problems, but already answered questions helped me to get by.
我遇到了一些 javascript 问题,但已经回答的问题帮助我度过了难关。 Although, there is an issue that I can't find a solution for it.
虽然,有一个问题我找不到解决方案。 My code is this:
我的代码是这样的:
con.query("SELECT * FROM posts WHERE post_type = 'post' ORDER BY `id` DESC LIMIT 10", function(err, result, fields) {
if (err) throw err;
var i;
var statuses = "[";
for (i = 0; i < 4; i++) {
username = result[i].poster;
message = result[i].message_text;
id = result[i].id;
post_type = result[i].post_type;
reply_to = result[i].reply_to;
statuses = statuses + '{"p":"1.jpg","s":0,"u":"' +username+ '","id":' + id + ',"m":"' + message + '"},'
}
statuses = statuses.substring(0, statuses.length - 1);
statuses = statuses + ']'
ws.send('{"action":"Load statuses","statuses":' + statuses + '}');
});
After the for loop , I want to take the whole statuses string variable that will look like this:在for 循环之后,我想获取看起来像这样的整个状态字符串变量:
{"p":"1.jpg","s":0,"u":"test","id":1,"m":"1st message"},{"p":"1.jpg","s":0,"u":"test","id":2,"m":"2nd message"},{"p":"1.jpg","s":0,"u":"test","id":3,"m":"3rd message"},{"p":"1.jpg","s":0,"u":"test","id":4,"m":"4th message"}
and add to it an object.并添加一个 object。 But not in the front or in the end of it.
但不是在前面或结尾。 Inside it.
在里面。 Like this:
像这样:
{"p":"1.jpg","s":0,"u":"test","id":1,"m":"1st message"},{"p":"1.jpg","s":0,"u":"test","id":123,"m":"The new message!"},{"p":"1.jpg","s":0,"u":"test","id":2,"m":"2nd message"},{"p":"1.jpg","s":0,"u":"test","id":3,"m":"3rd message"},{"p":"1.jpg","s":0,"u":"test","id":4,"m":"4th message"}
To cut a long story short, I would like to know if there is any function that adds object(s) next to others by their id.长话短说,我想知道是否有任何 function 通过他们的 id 在其他人旁边添加对象。
Example: statuses.addNewObject(nextToId, value)示例: statuses.addNewObject(nextToId, value)
By the code that you show inside for
, I understand that the value of result has a structure similar to this通过您在
for
中显示的代码,我了解到 result 的值具有与此类似的结构
const result = [
{
id: 1,
poster: "user 1",
message_text: "message 1",
post_type: "post",
reply_to: "***",
},
{
id: 2,
poster: "user 2",
message_text: "message 2",
post_type: "post",
reply_to: "***",
},
{
id: 3,
poster: "user 3",
message_text: "message 3",
post_type: "post",
reply_to: "***",
},
{
id: 4,
poster: "user 4",
message_text: "message 4",
post_type: "post",
reply_to: "***",
},
];
If it is the case, you can use map , to go through each element of the array and transform it according to the requirement you describe in the code.如果是这种情况,您可以使用map ,通过数组的每个元素到 go 并根据您在代码中描述的要求对其进行转换。 Please take a look at the following example
请看下面的例子
const result = [ { id: 1, poster: "user 1", message_text: "message 1", post_type: "post", reply_to: "***", }, { id: 2, poster: "user 2", message_text: "message 2", post_type: "post", reply_to: "***", }, { id: 3, poster: "user 3", message_text: "message 3", post_type: "post", reply_to: "***", }, { id: 4, poster: "user 4", message_text: "message 4", post_type: "post", reply_to: "***", }, ]; const output = result.map((entry) => ({ p: "1.jpg", s: 0, u: entry.poster, id: entry.id, m: entry.message_text, })); console.log(output);
See看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.