0

I have an object Like this

var Data= {
  "firstname": {
    "excelcolumnName": "firstName",
    "dbcolumn": "one",
    "testinfo": "basictest"
  },
  "labname": {
    "excelcolumnName": "LabName",
    "dbcolumn": "two",
    "testinfo": "basictest"
  },
  "percentCbd": {
    "excelcolumnName": "Percent CBD",
    "dbcolumn": "three",
    "testinfo": "Final Test"
  }

  "percentgeneral": {
    "excelcolumnName": "Percent General",
    "dbcolumn": "four",
    "testinfo": "Final Test"
  }
}

i want filter and create new object that contain testinfo:"basictest" the output of single object should be like this.

{
"one":"basictest",
"two":"basictest"
}

can any body help me on this.

3 Answers 3

1

From what I understand you want a single object containing dbcolumn:testinfo mappings

Check this JSBin

var Data2={};

_.map(Data,(obj)=>{
  if(obj.testinfo==='basictest'){
  var newObj={};
  newObj[obj.dbcolumn]=obj.testinfo;
  return _.assign(Data2,newObj);
  }
});
console.log(Data2);

The map function 'maps' each object into something else. The assign merges these key-value pairs. The newObj[obj.dbcolumn] line converts the value from 'dbcolumn:value' into a key for the new object.

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

2 Comments

WIth lodash, you can use function chaining on arrays - not sure if it works on objects like your Data - Check this link blog.mariusschulz.com/2015/05/14/…
With chaining, you could have done _(Data).filter(function to filter out only basic tests).map(to get only dbcolumn:testinfo mappings)
0
Var array = [];

for(var i=0;i<Object.keys(Data).length;i++){
  if(data[i].testinfo === 'basictest'){ 
    array.push(data[i]);
  }
}

array will have all objects with testinfo = 'basictest'

Comments

0

Try this

const Data = {
  "firstname": {
    "excelcolumnName": "firstName",
    "dbcolumn": "one",
    "testinfo": "basictest"
  },
  "labname": {
    "excelcolumnName": "LabName",
    "dbcolumn": "two",
    "testinfo": "basictest"
  },
  "percentCbd": {
    "excelcolumnName": "Percent CBD",
    "dbcolumn": "three",
    "testinfo": "Final Test"
  },
  "percentgeneral": {
    "excelcolumnName": "Percent General",
    "dbcolumn": "four",
    "testinfo": "Final Test"
  }
}

const result = {} // contain result

for (var i in Data) {
 const item = Data[i]
 result[item.dbcolumn] = item.testinfo // add a new object item
}

console.log(result) // print {one: "basictest", two: "basictest", three: "Final Test", four: "Final Test"}

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.