2

I have a requirement to convert an array to object based on values in another array using lodash or underscore js.

var connections = [ 'facebook', 'twitter', 'linkedin', 'xing', 'weibo' ];

var contents = [
    {
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner1.jpg",
        preview_title: "title of the message1",
        facebook: "fb first 22 once more",
        preview_description: "Home↵How to↵How to find a Place in KeralaHow to find a place in Kerala"
    },
    {
        linkedin: "test linkedin",
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner2.jpg",
        preview_title: "linkedin title2",
        preview_description: "linkedin description 2"
    },
    {
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner3.jpg",
        preview_title: "linkedin title3",
        preview_description: "linkedin description 3",
        twitter: "test twitter"
    }
];

My requirement is to loop through connections array and if any of connections present in contents array create a result ojbect with those connection name.

Expected result

var result = {
    facebook: {
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner1.jpg",
        preview_title: "title of the message1",
        facebook: "fb first 22 once more",
        preview_description: "Home↵How to↵How to find a Place in KeralaHow to find a place in Kerala"
    },
    linkedin: {
        linkedin: "test linkedin",
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner2.jpg",
        preview_title: "linkedin title2",
        preview_description: "linkedin description 2"
    },
    twitter: {
        preview_image_url: "http://www.ideacellular.com/ISC/images/banners/home-page/banner3.jpg",
        preview_title: "linkedin title3",
        preview_description: "linkedin description 3",
        twitter: "test twitter"
    }
};

Your help is much appreciated. I have tried some usual looping using forEach, is there any simple way to do it using underscore js or lodash. Thank you.

2 Answers 2

1

Try this,

var result = {};

connections.forEach(function (key) {
    contents.forEach(function (el) {
        if (el[key] && !result[key]) {
            result[key] = el;
        }
    });
});

console.log(result);

Example

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for you help. God bless you.
1

You could use indexBy() and intersection() for this:

_.indexBy(contents, function(item) {
    return _(item)
        .keys()
        .intersection(connections)
        .first();
});

The idea is that you find the intersection between the keys of the contents item, and the connections array. If such an intersection exists, you use that value as the indexing key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.