首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lodash从另一个对象中追加对象而不进行替换

Lodash从另一个对象中追加对象而不进行替换
EN

Stack Overflow用户
提问于 2016-12-06 10:53:18
回答 2查看 290关注 0票数 1

如何在不替换的情况下从另一个对象向对象追加值?

我正在尝试将一个值从ObjectA附加到ObjectB,作为子对象的名称。

我是ObjectA.

可以看出,ObjectA有4个子对象,每个子对象都与一个uid关联。

我是ObjectB

所以我试着把每个孩子的uid从ObjectA映射到ObjectB

这就是理想代码的样子

代码语言:javascript
复制
Object.keys(ObjectA).forEach(
    (key) => _.assign(ObjectB, {'Product 07': true})
)

我试图将每个ObjectB迭代注入到源占位符ES6字符串内插,但是它会失败,就像这样。

代码语言:javascript
复制
Object.keys(ObjectA).forEach(
    (key) => _.assign(ObjectB, {`${basketObject[key].uid}`: true})
)

Source(JSON)

请使用此代码使用_.mapValues将对象对象转换为数组对象,以从对象名称生成uid。

代码语言:javascript
复制
var ObjectA = _.mapValues(data, (val, index) => {
    return { ...val, index }
})


{
"Product": {
    "Product 01": {
        "approved": true,
        "brand": "MANGO",
        "category": "Woman",
        "color": "Black,Bright Pink",
        "description": "Rounded neck Chain detail Slit detail Zip fastening on the back section Inner lining · Side length 117.0 cm · Back length 135.5 cm These measures have been calculated for a S size.",
        "imageURL": "https://s11.postimg.org/612lpusf7/73025590_80.jpg",
        "name": "Chain Collar Dress",
        "price": 499,
        "purchasedUser": {
            "mgzQBxjF9NNVACh18ID1xDPtrwH2": true
        },
        "size": "S,M,L,XL"
    },
    "Product 02": {
        "approved": true,
        "brand": "MANGO",
        "category": "Man",
        "color": "Grey",
        "description": "Modern coat that suits every man",
        "imageURL": "https://s4.postimg.org/vub6umtlp/73079700_99.jpg",
        "name": "Tailored wool-blend overcoat",
        "price": 899,
        "purchasedUser": {
            "mgzQBxjF9NNVACh18ID1xDPtrwH2": true
        },
        "size": "S,M,L,XL"
    },
    "Product 03": {
        "approved": true,
        "brand": "MANGO",
        "category": "Kid",
        "color": "Dark Navy, Offwhite, Light Pink,Medium Heather Grey",
        "description": "Cotton fabric Spots print Rounded neck Short sleeve Embroidered Mango logo Decorated with sequins",
        "imageURL": "https://s16.postimg.org/xkpg8qypx/73003605_56.jpg",
        "name": "Logo cotton t-shirt",
        "price": 29.9,
        "purchasedUser": {
            "mgzQBxjF9NNVACh18ID1xDPtrwH2": true
        },
        "size": "3-4 years,5-6 years, 7-8 years, 9-10 years, 11-12 years, 13-14 years"
    },
    "Product 04": {
        "approved": true,
        "brand": "UNIQLO",
        "category": "Woman",
        "color": "Red, Natural, Navy",
        "description": "This comfortable women's shirt features the distinctive soft, warm, fuzzy feel of brushed 100% cotton flannel material. It looks great as a shirt or light outer layer, and the slightly loose cut gives it a relaxed style. The classic casual plaid design is available in a variety of colors.",
        "imageURL": "https://s11.postimg.org/48sak0lmr/69_180861.jpg",
        "name": "Flannel Check Long Sleeve Shirt",
        "price": 79.9,
        "size": "S,M,L,XL"
    },
    "Product 05": {
        "approved": true,
        "brand": "UNIQLO",
        "category": "Kid",
        "color": "Offwhite",
        "description": "Star Wars™  A long time ago in a galaxy far, far awayLong after the Jedi Knights have perished, young Luke Skywalker has a fateful encounter with Princess Leia, a leader of the Rebel forces. Luke and Leia fight alongside Han Solo, Chewbacca, R2-D2 and C-3PO against the evil forces of Darth Vader to help restore peace in the galaxy.  starwars.com  © & ™ Lucasfilm Ltd.  MATERIAL100% Cotton  CAREMachine wash cold",
        "imageURL": "https://s21.postimg.org/xdojmkx1z/01_185878.jpg",
        "name": "BOYS STAR WARS Short Sleeve Graphic T-Shirt",
        "price": 39.9,
        "size": "110cm,120cm,130cm,140cm,150cm"
    },
    "Product 06": {
        "approved": true,
        "brand": "UNIQLO",
        "category": "Man",
        "color": "White,Dark Grey, Navy",
        "description": "This amazing men's polo is made from next-generation, high-tech DRY-EX material. It dries sweat incredibly quickly to keep you feeling smooth, dry, and comfortable. The soft, cotton-like texture and cut allow for easy movement and combined, offer for superb comfort. In a fashionable striped pattern, perfect for warm-weather casual looks.  MATERIAL[08Dark Gray] Body: 96% Polyester, 4% Nylon/ Rib: 100% Polyester [Other Colors] 100% Polyester  CARE[08Dark Gray] Machine wash cold, gentle cycle [Other Colors] Machine wash cold",
        "imageURL": "https://s11.postimg.org/bvm6i0ujn/00_169374.jpg",
        "name": "MEN Dry EX Short Sleeve Polo Shirt",
        "price": 99.9,
        "size": "S,M,L,XL"
    },
    "Product 07": {
        "brand": "FILANTO",
        "category": "Woman",
        "color": "Black",
        "description": "Dress to impress this festive season with our must-have dresses. Our favourite piece for for creating effortlessly chic and glamorous look comes in the form of this delicate lace number. A form-fitting silhouette hugs and flatter your curves beautifully while its minimalist design makes it easy to dress it up or down.",
        "imageURL": "https://s15.postimg.org/5qqq9496j/r1_top_fl20131894_002_a_btm_a_fl20131894.jpg",
        "name": "Casual Long Dress",
        "price": 59.9,
        "size": "S,M,L,XL"
    },
    "Product 08": {
        "brand": "HOTSHOTS",
        "category": "Kid",
        "color": "Off White",
        "description": "Update her dress collection with this beautiful printed piece. Offering a short-sleeve design, round neckline and keyhole back button fastening. This dress also showcases a vibrant butterfly print motif. Sweet and demure, all this piece needs is a pair of flats to complete the look.",
        "imageURL": "https://s16.postimg.org/ocebpd3h1/ho20129717_002_e_ho20129717.jpg",
        "name": "Casual Short Dress Knit",
        "price": 29.9,
        "size": "4-5,6-7,8-9,10-11"
    },
    "Product 09": {
        "approved": true,
        "brand": "PADINI",
        "category": "Man",
        "color": "White",
        "description": "Steeped in a solid white wash, this classic Oxford shirt gets a modern tweak with the addition of stripes across the body. It also features the iconic shirt collar, along with a chest pocket, buttoned cuffs and raised placket. This is the ultimate piece for curating both casual and formal looks.",
        "imageURL": "https://s11.postimg.org/cvmbgxg2r/r1_top_pd20131998_003_b_btm_pd20131965_002_b_pd2.jpg",
        "name": " Casual Long Sleeve Shirt",
        "price": 89,
        "size": "XS,S,M,L"
    },
    "Product 10": {
        "brand": "PONEY",
        "category": "Kid",
        "color": "Navy",
        "description": "This navy polo shirt designed with knight prints is Poney's new wardrobe classic. Made from 100% Cotton Fully Comb and Single Jersey. Features a neat polo collar, brown buttons, neckline lining in contrast fabric and leather patches.",
        "imageURL": "https://s17.postimg.org/bcpbfwrcf/6j6a5351_2_1_1.jpg",
        "name": "KNIGHT POLO SHIRT",
        "price": 119,
        "size": "7-8,9-10,11-12"
    },
    "Product 11": {
        "brand": "PONEY",
        "category": "Kid",
        "color": "Navy",
        "description": "This beautiful daisy print blouse with contrast sleeves is perfect for your best friend's birthday party. Pair with our \"Poney Daisy Print Jeans\" for a chic contemporary look or our \"Poney Tutu Skirt\" for a classic party look.",
        "imageURL": "https://s12.postimg.org/la1o7vtm5/6j6a5327_1.jpg",
        "name": "DAISY PRINT BLOUSE",
        "price": 76,
        "size": "1-2,2-3,3-4,4-5,5-6"
    },
    "Product 12": {
        "brand": "GAMESTERS",
        "category": "Kid",
        "color": "Grey",
        "description": "Ensure your little one is clothed in comfort and style with this super-cool and adorable jogger shorts from Gamester. Complete with an allover repetitive print, elasticated-drawstring waist, two front side pockets and one back pocket. Complement this shorts with any printed tee to complete his look.",
        "imageURL": "https://s14.postimg.org/tz3xvg3z5/gm20130687_002_e_gm20130687.jpg",
        "name": "Casual Knit Shorts",
        "price": 29.9,
        "size": "6-7"
    }
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-06 15:30:54

它应该能工作

代码语言:javascript
复制
_.mapValues(ObjectA, (val) => {
    ObjectB[val.uid] = true;
})
票数 1
EN

Stack Overflow用户

发布于 2016-12-06 11:07:52

您可以使用reduce将对象映射到另一个对象;

代码语言:javascript
复制
var objects = [ // Your objects, this is just dummy data
    { uuid: 'Product 01', approved: true },
    { uuid: 'Product 02', approved: true },
    { uuid: 'Product 03', approved: true }
]

var items = objects.reduce(function (prev, current) {
    prev[current.uuid] = current.approved;
    return prev;
}, {});
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40993587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档