简体   繁体   中英

How to add array values to an object

I have an array with few values. I want to iterate over array and add those values to an object as value starting from second object element which has null value. I cannot figure out how can I do it properly. Here is my code

let objectParameters = {
      "current_lang" : currentLang,
      "project_name" : null,
      "project_type" : null,
      "min_price"    : null,
      "max_price"    : null
}

let arrayValues = ["Project name", "Project Type", 150, 950];

arrayValues .forEach(function(item) {

 //Add array value to an object
}

Desired output

let objectParameters = {
      "current_lang" : currentLang,
      "project_name" : "Project name",
      "project_type" : "Project Type",
      "min_price"    : 150,
      "max_price"    : 950
}

You can get all the properties of an object with

Object.keys(objectParameters)

as an array and assign them values from your arrayValues

Like that:

 let objectParameters = { "current_lang" : "en", "project_name" : null, "project_type" : null, "min_price" : null, "max_price" : null } let arrayValues = ["Project name", "Project Type", 150, 950]; let params = Object.keys(objectParameters); for(let i = 1; i < params.length; i++) { objectParameters[params[i]] = arrayValues[i-1]; } console.log(objectParameters); 

Came up with this :

 let objectParameters = { "current_lang" : "currentLang", "project_name" : null, "project_type" : null, "min_price" : null, "max_price" : null }; let arrayValues = ["Project name", "Project Type", 150, 950], keys = Object.keys(objectParameters); keys.shift() // Removing the first key, which is not null keys.forEach( (key,i) => objectParameters[key] = arrayValues[i]) console.log(objectParameters) 

Using for in loop to iterate through the object and shift() to get the first array element each iteration, in all cases we rely on the order, that is probably not a good approach.

 let objectParameters = { "current_lang" : "currentLang", "project_name" : null, "project_type" : null, "min_price" : null, "max_price" : null } let arrayValues = ["Project name", "Project Type", 150, 950]; for(let p in objectParameters){ if(!objectParameters[p]) objectParameters[p] = arrayValues.shift() } console.log(objectParameters) 

I don't see necessary to use hasOwnProperty in this case.

If you know the index of the corresponding value in the array you dont need the loop and can just do:

objectParameters["project_name"] = arrayValues[0]
objectParameters["project_type"] = arrayValues[1]  
...

if you have the array values when youre creating the object you can just use them at the object creation time:

let objectParameters = {
      "current_lang" : "en",
      "project_name" : arrayValues[0],
      ...
}

Well, if you got ordered array and object, here this code might help you:

        var obj = {
            'first': '1',
            'second': '2'
        };

        var arr = ['first', 'sec'];

        Object.keys(obj).forEach(function (key, arrIndex) {
           obj[key] = arr[arrIndex];
        });

Then you edit and optimize it the way you want. Little improvement, thank to @Jeremy Thille

If your arrayValues format value will be same then you could use separate keys and use reduce() method for your arrays values and Destructuring_assignment .

DEMO

 const keys = ["project_name", "project_type", "min_price", "max_price"], arrayValues = ["Project name", "Project Type", 150, 950]; let result = arrayValues.reduce((r, v, i) => { return { ...r, [keys[i]]: v } }, {current_lang:navigator.language}); console.log(result) 
 .as-console-wrapper {max-height: 100% !important;top: 0;} 

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