简体   繁体   中英

merge two javascript objects to new key : value object

What is the best way to merge these two objects into one and add keys as well. I tried a lot already but I am lost with how javascript works on objects and arrays... Who can guide me into the right direction? Either with plain JS or jquery.

Object 1

["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"]

Object 2

["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"]

Desired object

[[Price="EUR 1,99",Name="Popken"],[Price="EUR 0,99",Name="Lucky Animal"],[Price="EUR 4,99",Name="mein-terrarium"], etc....]  
var newList = [];

for(var i=0; i<obj1.length; i++){
    newList.push({
        Price: obj1[i],
        Name: obj2[i]
    });
}

This has no error handling, and there may be a better way using the map function of the Array, but this should work as long as the length of both of your original objects are the same.

Should be pretty straightforward. Assuming arr1 is your array with prices, and arr2 is your array with names:

var newarr = [],
    len = (number of items);

for(var i=0;i<len;i++){
    newarr.push({
        Price:arr1[i];
        Name:arr2[i];
    });
}

Solution using the zip-function in underscore.js

var prices = ["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"];

var products = ["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"];

var result = _.map(_.zip(products, prices), function(arr) {
    return {
        Price: arr[1],
        Name: arr[0]
    };
});


console.log(result);

Here is a generic solution--it will "fold" an object so that you get a list of objects instead of an object of lists.

function refold_list_from_object(data) {
    "use strict"    
    var keys = Object.keys(data),
        descriptor = {configurable: true, enumerable: true, writable: true, value: null},
        properties = keys.reduce(function(p, k){
            p[k] = descriptor
            return p
        }, {}),
        nrecords = Math.max.apply(null, keys.map(function(k){return data[k].length})),
        records  = [], vi
    function makerecord() {
        return Object.create(null, properties)
    }
    function record_from_value_index(vi) {
        return keys.reduce(function(obj, k) {
            obj[k] = data[k][vi] || null
            return obj
        }, makerecord())
    }
    for (vi = 0; vi < nrecords; vi++) {
        records.push(record_from_value_index(vi))
    }
    return records
}

data should be an object with lists indexed by their field names:

var data = {
    Price: ["EUR 1,99", "EUR 0,99", "EUR 4,99", "EUR 2,29", "EUR 1,43", "EUR 1,60", "EUR 1,79", "EUR 1,79", "EUR 1,39", "EUR 6,30", "EUR 1,43", "EUR 1,78", "EUR 1,90", "EUR 1,24", "EUR 1,41"],
    Name: ["Popken", "Lucky Animal", "mein-terrarium", "zooup", "zoofair", "XL-Hundeshop", "tiierisch-de", "Zoo Galaxie", "Petshop", "Danto GmbH", "Heimtierbedarf-Mazinke", "TIERKOSMOS", "Gazoma", "Zooheld", "dasko24"],
    // An extra field to demonstrate padding:
    Description: ['Foo', 'Bar', 'Baz']
}

var recordlist = refold_list_from_object(data)

recordlist will look like this:

[
  {"Price":"EUR 1,99", "Name":"Popken",         "Description":"Foo"},
  {"Price":"EUR 0,99", "Name":"Lucky Animal",   "Description":"Bar"},
  {"Price":"EUR 4,99", "Name":"mein-terrarium", "Description":"Baz"},
  {"Price":"EUR 2,29", "Name":"zooup",          "Description":null},
  {"Price":"EUR 1,43", "Name":"zoofair",        "Description":null},
  {"Price":"EUR 1,60", "Name":"XL-Hundeshop",   "Description":null},
  {"Price":"EUR 1,79", "Name":"tiierisch-de",   "Description":null},
  {"Price":"EUR 1,79", "Name":"Zoo Galaxie",    "Description":null},
  {"Price":"EUR 1,39", "Name":"Petshop",        "Description":null},
  {"Price":"EUR 6,30", "Name":"Danto GmbH",     "Description":null},
  {"Price":"EUR 1,43", "Name":"Heimtierbedarf-Mazinke", "Description":null},
  {"Price":"EUR 1,78", "Name":"TIERKOSMOS",     "Description":null},
  {"Price":"EUR 1,90", "Name":"Gazoma",         "Description":null},
  {"Price":"EUR 1,24", "Name":"Zooheld",        "Description":null},
  {"Price":"EUR 1,41", "Name":"dasko24",        "Description":null}
]

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