1

I have a json object that I need to convert in javascript using lodash:

{
    "BidNumber": 2,
    "BidResult": 1,
    "BidAmount": "6756",
    "BidData": [
        {
            "name": "JonSnow",
            "Data": "Standard data for Jon Snow"
        },
        {
            "LineNum": "HarryPotter",
            "Data": "Standard data for Jon Snow"
        },
        {
            "LineNum": "MickyMouse",
            "Data": "Standard data for Micky Mouse"
        }
    ],
    "BidReference": "22e06e66-e711-bd14-7874a-002219649f24"
} 

I want to convert that to:

{
    "bidNumber": 2,
    "bidResult": 1,
    "bidAmount": "6756",
    "bidData": {
        "jonSnow": "Standard data for Jon Snow",
        "harryPotter": "Standard data for Jon Snow",
        "mickyMouse": "Standard data for Micky Mouse"
    },
    "bidReference": "22e06e66-e711-bd14-7874a-002219649f24"
}

Can't figure it out the way I can do it in lodash (including camelCase part)

3
  • Does it have to be with lodash? This is trivial in vanilla Javascript Commented Feb 13, 2018 at 15:44
  • Yes, because that collection is much larger in reality. Commented Feb 13, 2018 at 15:45
  • 1
    Yes, sorry it should be "Standard data for Harry Potter". Good spot, thanks. Commented Feb 13, 2018 at 15:50

2 Answers 2

2

Can be easily done with JS..

data.BidData = Object.assign({}, ...data.BidData.map(el => { 
  return { [(el.name ? el.name : el.LineNum)]: el.Data } 
}));

You dont need to use loadash unless you want to use _.extend

var data = {
    "BidNumber": 2,
    "BidResult": 1,
    "BidAmount": "6756",
    "BidData": [
        {
            "name": "JonSnow",
            "Data": "Standard data for Jon Snow"
        },
        {
            "LineNum": "HarryPotter",
            "Data": "Standard data for Jon Snow"
        },
        {
            "LineNum": "MickyMouse",
            "Data": "Standard data for Micky Mouse"
        }
    ],
    "BidReference": "22e06e66-e711-bd14-7874a-002219649f24"
};

data.BidData = Object.assign({}, ...data.BidData.map(el => { 
  return { [jslcfirst(el.name ? el.name : el.LineNum)]: el.Data } 
}));

console.log(data);

function jslcfirst(string) 
{
    return string.charAt(0).toLowerCase() + string.slice(1);
}

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

6 Comments

@Archer looks fine now!
That looks really great! Is it possible to lowercase that part within the map() function?
You don't need the function any more, since the field name is always the same.
Quick update, we have camelCase only for BidData... how to do it with all keys in that object?
@Quang jslcfirst this function lowercases the first character keeping all the other characters same. So ideally it solves that. Also as it answers your question please mark the answer correct!
|
0

Using Lodash:

obj.BidData = _.reduce(obj.BidData, function(flatObj, item) {
    flatObj[_.camelCase(item.name || item.LineNum)] = item.Data;
    return flatObj;
},{});

But you would not necessarily need lodash(underscore), using vanilla JS this is easily achievable

function convertToCamelCase(string) {
    return string.charAt(0).toLowerCase() + string.slice(1);
}

obj.BidData = obj.BidData.reduce(function(flatObj, item) {
    flatObj[convertToCamelCase(item.name || item.LineNum)] = item.Data;
    return flatObj;
},{})

3 Comments

That is truly awesome! Thank you.
actually I'm getting [ts] error: Property 'LineNum' does not exists on type {}...#
Which means one of your items on BidData array isn't having either of 'name' or 'LineNum' field

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.