search

Home  >  Q&A  >  body text

javascript - How to take out the key value whose key value is true in object, and then join(',')

How to take out the key value in the object whose key value is true, and then join(','). The current data structure is, ,
send_message:{1: true,2:true}, the data structure I want to give to the background is: send_message=1,2&is_live=1

漂亮男人漂亮男人2713 days ago813

reply all(2)I'll reply

  • 为情所困

    为情所困2017-06-26 10:55:52

    Use for...in...:
    This way:

    const message = { 1: true, 2: true, 3: false };
    const arr = [];
    for (let item in message) {
        if (message[item]) {
            arr.push(item);
        }
    }
    console.log(arr.join(','));

    reply
    0
  • 某草草

    某草草2017-06-26 10:55:52

    Object.keys(message).filter(k => message[k] === true).join(',')

    reply
    0
  • Cancelreply