简体   繁体   中英

JSON - How do I add object in an objectArray string?

I'm kinda new to javascript. Few days ago I started a node js project. I had some javascript problems, but already answered questions helped me to get by. 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:

{"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. 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.

Example: 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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