Transform a JSON object keys and values to lower case


Sep 08, 2021
featured image
      
export const objToLowerCase = (obj) => {
  if (!obj) return obj;
  return Object.keys(obj).reduce((prev, current) => ({ ...prev, [current.toLowerCase()]: obj[current].toLowerCase() }), {});
};

      
    
      

  test("obj to lowercase", () => {
    const obj = {
      MyKey1: "MY Value 1",
      MyKey2: "MY Value 2",
    };
    const result = objToLowerCase(obj);
    expect(result["MyKey1"]).toBeDefined();
    expect(result["MyKey1"]).toBe("my value 1");
    expect(result["MyKey2"]).toBeDefined();
    expect(result["MyKey2"]).toBe("my value 2");
  });



      
    

Photo by Brett Jordan on Unsplash


js utils dev