Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.20.1/ramda.min.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var fieldvalues = { name: "hello there", mobile: "1234", meta: {status: "new"}, 
                      comments: [ {user: "john", comment: "hi"}, 
                               {user:"ram", comment: "hello"}]
                    };
// Objective: Convert this to a dotted notation like this
// { name: "hello there", .... "meta.status": "new", "comments.0.user": "john"
//   "comments.0.comment": "hi", "comments.1.user": "ram"......}
// This needs to be done using Functional Programming (using Ramda in this example)
var _toDotted = function(acc, obj) {
  var key = obj[0], val = obj[1];
  
  if(typeof(val) != "object") {  // Matching name, mobile etc
    acc[key] = val;
    return acc;
  }
  
  if(!Array.isArray(val)) {     // Matching meta
    for(var k in val)
      acc[key + "." + k] = val[k];
    return acc;
  }
  
  // Matching comments
  for(var idx in val) {
    for(var k2 in val[idx]) {
      acc[key + "." + idx + "." + k2] = val[idx][k2];
    }
  }
  return acc;
};
var toDotted = R.pipe(R.toPairs, R.reduce(_toDotted, {}));
console.log(toDotted(fieldvalues));
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
rsmoorthypro
0viewers