Convert JSON to Array of Objects using lodash

lodash to convert object to array
lodash get value by key from array of objects
object to array of values lodash
convert object to array of object lodash
transform object into array lodash
lodash make object to array
lodash flatten object into array
lodash compare objects

I have a JSON object in NoSql database in this format. We are getting this data after migrating some records from some other database and these are multi-valued fields.(Basically we are trying to clean the data for further processing).

{
    "BPContName":"aName;bName;cName",
    "BPContEmail":"aEmail;bEmail;cEmail",
    "BPContPWID":"aPWID;bPWID;cPWID"
}

I want to add another key "bpTableDataName" in the same JSON which should have this format and values,

"bpTableDataName": [
    {
      "name": "aName",
      "email": "aEmail",
      "pwdid": "aPWID"
    },
    {
      "name": "bName",
      "email": "bEmail",
      "pwdid": "bPWID"
    },
    {
      "name": "cName",
      "email": "cEmail",
      "pwdid": "cPWID"
    }
  ],

Is there a way we can achieve this using lodash?

You can use split() to split the values into an array. Then iterate over the array and create the require json and then push that into results.

Check this out.

var data = {
    "BPContName":"aName;bName;cName",
    "BPContEmail":"aEmail;bEmail;cEmail",
    "BPContPWID":"aPWID;bPWID;cPWID"
}
var names = data.BPContName.split(';');
var emails = data.BPContEmail.split(';');
var pwids = data.BPContPWID.split(';');

var results = [];

for(var i = 0 ; i < names.length; i++) {
  var obj = {
    name: names[i],
    email: emails[i],
    pwdid: pwids[i]
  }
  results.push(obj);
}

console.log(results)

Convert JSON to Array of Objects using lodash, 问题: I have a JSON object in NoSql database in this format. We are getting this data after migrating some records from some other database� convert array to object the output should be same as key and value. sample array:(my input structure) var a = [1,2,3,4,5]; I need this structure of output:

Try following code -

o = {
  "BPContName": "aName;bName;cName",
  "BPContEmail": "aEmail;bEmail;cEmail",
  "BPContPWID": "aPWID;bPWID;cPWID"
}

map = { "BPContName" : "name", "BPContEmail": "email", "BPContPWID": "pwdid" }

const result = _.reduce(o, (arr, v, k) => ( v.split(";").forEach((x,i) => _.set(arr, `${i}.${map[k]}`, x)), arr ), [])
   
console.log(result)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

Convert object array to hash map using lodash, I think you are looking for _.keyBy (or _.indexBy in older versions) _.keyBy(data, ' rel');. i. NET Documentation JSON with dynamic. I have 2 arrays containing objects, each object has multiple properties but one of them is an ID that is unique. a new object is created) and it can also be used to combine multiple objects together. Convert object array to hash map using lodash - Blogger 67 11. Object[]), has a fixed size.

You could reduce the entries returned by Object.entries like this:

let obj = {
  "BPContName": "aName;bName;cName",
  "BPContEmail": "aEmail;bEmail;cEmail",
  "BPContPWID": "aPWID;bPWID;cPWID"
}

let bpTableDataName = Object.entries(obj).reduce((r, [key, value]) => {
  let splits = value.split(";");
  key = key.replace("BPCont", "").toLowerCase();
  splits.forEach((split, i) => (r[i] = r[i] || {})[key] = split)
  return r;
}, [])

obj.bpTableDataName = bpTableDataName;

console.log(obj)

Feature request: _.toObject() to convert array to object � Issue #2718 , how to create object with keys from array, and values from a function? the lodash module in npm, we can select the required felid in JSON� I have a JSON object in NoSql database in this format. We are getting this data after migrating some records from some other database and these are multi-valued fields.(Basically we are trying to c

Using Object.assign, Object.entries, Array#map and the spread operator make this trivial

    const inputdata = {
        "BPContName":"aName;bName;cName",
        "BPContEmail":"aEmail;bEmail;cEmail",
        "BPContPWID":"aPWID;bPWID;cPWID"
    };

    const t1=Object.assign({},...Object.entries(inputdata).map(([k,v])=>({[k]:v.split(';')})));
    inputdata.bpTableDataName=t1.BPContName.map((name,i)=>({name,email:t1.BPContEmail[i],pwdid:t1.BPContPWID[i]}));
    console.log(inputdata);

Documentation, Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order objects for ['barney', 'fred', 'pebbles'] Converts all elements in array into a string separated by separator . The Lodash _.isArrayLikeObject() method checks the given value is an Array-like Object or not. This method is similar to _.isArrayLike() method except that it also checks if the value is an object or not. Syntax: _.isArrayLikeObject( value ) Parameters: This method accepts a single parameter as mentioned above and described below:

Basically what you need is to zip it.

Snippet:

let obj = {"BPContName":"aName;bName;cName","BPContEmail":"aEmail;bEmail;cEmail","BPContPWID":"aPWID;bPWID;cPWID"},
    res = _.zipWith(
      ..._.map(obj, v => v.split(';')),
      (name, email, pwid) => ({name, email, pwid})
    );
    
console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

uniq(array, Creates a slice of array with n elements dropped from the beginning. If an object is provided for predicate the created _.matches style callback returns throttle , thru , times , toArray , toPlainObject , transform , union , uniq , unshift , unzip� When using the JSON.parse() on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object. Example The JSON returned from the server is an array:

lodash get all value from array of objects Code Example, Get code examples like "lodash get all value from array of objects" instantly right from your google search results with the Grepper Chrome Extension. _.differenceBy(array, [values], [iteratee=_.identity]) source npm package. This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared.

convert array object to string and use it using lodash in javascript , Get code examples like "convert array object to string and use it using lodash in javascript" instantly right from your google search results with the Grepper� The output of the Parse is an indexed array, but Select is designed to Select based on a Key value. you can access the values in the array directly using JSON notation in a compose statement. If you will send a sample copy of your output used in the Parse I can explain what the JSON notation would look like.

Convert object to array of objects using lodash 3, Convert object to array of objects using lodash 3 lodash find object in array map always returns array type, and you can use both value and key in iterator: of an object and subobjects when for e.g. talking to some json api which uses� I'm getting these JSON objects from an API call that I'd like to convert into an array of objects (Users). I've tried replacing the "users" object brackets to square brackets but then it doesn't like the IDs and ":" before each array item. not sure how to replace those, as they can be any number and have any number of users returned.

Comments
  • you can do it in native javascript too
  • Welcome to Stack Overflow! Please take the tour (you get a badge!) and read through the help center, in particular How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Note: The fact it starts out as JSON is irrelevant. By the time you've parsed it, it's just an object, not a "JSON object."
  • @JaromandaX I am new to JavaScript actually. Can you please tell me how?
  • You have PWID in the original object but pwdid with an extra d in the output. Is that a spelling error?
  • @KirtiJha I have updated my answer to suit your use case.
  • ReferenceError: _ is not defined ... fail
  • You must import lodash first : const _ = require('lodash')
  • ReferenceError : require is not defined ... still fail - don't make a runnable snippet that doesn't run - it doesn't look good
  • oh that's no good at all, you're basing the key on the data!! I think aName;bName etc are example place holders for such names as Adam, Brian, Charlie etc
  • I was going along your lines too, until I saw that :p