jquery - Assign object value as array in javascript - Stack Overflow

How to assign the value to array in object value? It may has multiple input ing in and expected the inp

How to assign the value to array in object value? It may has multiple input ing in and expected the input appended to array.

Code:

var ob = {};
$.each( input, function( key, value ) {
    var v = [];
    ob[key] = v.push(value);
      console.log( v );     
      console.log( "obj: " + ob );                          
      console.log( key + ": " + value );
    });

Input:

First input- {A: "34",B: "2"}
Second input- {A: "21",B: "11"}

Expected:

ob = {A: ["34","21"] ,B: ["2","11"]}

How to assign the value to array in object value? It may has multiple input ing in and expected the input appended to array.

Code:

var ob = {};
$.each( input, function( key, value ) {
    var v = [];
    ob[key] = v.push(value);
      console.log( v );     
      console.log( "obj: " + ob );                          
      console.log( key + ": " + value );
    });

Input:

First input- {A: "34",B: "2"}
Second input- {A: "21",B: "11"}

Expected:

ob = {A: ["34","21"] ,B: ["2","11"]}
Share Improve this question asked Jan 23, 2019 at 8:16 JohnnyCcJohnnyCc 5251 gold badge8 silver badges24 bronze badges 1
  • 5 var v = []; empties v on each iteration step. – connexo Commented Jan 23, 2019 at 8:19
Add a ment  | 

3 Answers 3

Reset to default 2

Hope this helps,

var ob = {};

$.each(input, function(key, value) {
    if (!ob[key]) {
        ob[key] = [];  // Creates a new Array for the key, if no array is there
    }
    ob[key].push(value);  // Pushes the value to the array of the particular key
});

Create a function and an object variable. Check if the key exist in that object. If it does not exist they create the key and push the values

let input1 = {
  A: "34",
  B: "2"
}
let input2 = {
  A: "21",
  B: "11"
}

// a object which will hold the key and value

let finalObj = {};
// create a function which will be called o add key an value property to object
function createObj(obj) {
  // iterate the object
  for (let keys in obj) {
    // check if final object has the relevent key
    if (finalObj.hasOwnProperty(keys)) {
      // if it has that key then push the value according to the key
      finalObj[keys].push(obj[keys])
    } else {
      finalObj[keys] = [obj[keys]]
    }
  }

}

createObj(input1)
createObj(input2)
console.log(finalObj)

The problem is v empties on each iteration, because of this line:

var v = [];

Try doing this instead:

$.each(input, (key, val) => {
    if (ob[key]) {
        ob[key].push(val);
    } else {
        ob[key] = [val];
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745487397a4629826.html

相关推荐

  • jquery - Assign object value as array in javascript - Stack Overflow

    How to assign the value to array in object value? It may has multiple input ing in and expected the inp

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信