JS clone object with only one property

Viewed 99

I have an object that looks like this:

  {
    accord: {
      model_type: 'Car',
      totalCapacity: 4,
      maxSpeed:170,
      kms: 140000
    },
    ram: {
      model_type: 'Car',
      totalCapacity:5,
      maxSpeed: 220
      kms: 14000
    },
    corvette: {
      model_type: 'Car',
      totalCapacity: 2,
      maxSpeed: 300,
      kms: 50000,
    }
  }

and I am trying to copy this object into one that looks like this:

  {
    accord: {
      maxSpeed:170,
    },
    ram: {
      maxSpeed: 220
    },
    corvette: {
      maxSpeed: 300,
    }
  }

Ive tried cloning it to an array with object keys and making an object using those values but it doesnt seem to work. I feel like there is a much easier way to do this though

Each object in the main object(accord, ram, corvette) can be different everytime and there can be more than 3 or less than 3 as well

5 Answers
Object.fromEntries(Object.entries(obj).map(([key, value]) => [
  key, 
  Object.fromEntries(
    Object.entries(value)
      .filter(([key, value]) => key === "maxSpeed")
  )
]))

You can use Object.entries() to iterate each key & value pairs to then generate your object using array#reduce.

const cars = { accord: { model_type: 'Car', totalCapacity: 4, maxSpeed: 170, kms: 140000 }, ram: { model_type: 'Car', totalCapacity: 5, maxSpeed: 220, kms: 14000 }, corvette: { model_type: 'Car', totalCapacity: 2, maxSpeed: 300, kms: 50000, } },
      result = Object.entries(cars).reduce((r, [k, {maxSpeed}]) => {
        r[k] = { maxSpeed };
        return r;
      },{});
console.log(result);

You could clone the top-level object and the car objects (deep clone) like this:

const input = {
  accord: {
    model_type: "Car",
    totalCapacity: 4,
    maxSpeed: 170,
    kms: 140000,
  },
  ram: {
    model_type: "Car",
    totalCapacity: 5,
    maxSpeed: 220,
    kms: 14000,
  },
  corvette: {
    model_type: "Car",
    totalCapacity: 2,
    maxSpeed: 300,
    kms: 50000,
  },
};

const extractProperty = (prop) => ({ [prop]: val }) => ({ [prop]: val });

const extractMaxSpeed = extractProperty("maxSpeed");

const result = Object.fromEntries(
  Object.entries(input).map(([key, val]) => [key, extractMaxSpeed(val)])
);

console.log(result);

With the help of Object.entries and reduce, the following would work for your use case :-

const input = {
  accord: {
    model_type: 'Car',
    totalCapacity: 4,
    maxSpeed: 170,
    kms: 140000
  },
  ram: {
    model_type: 'Car',
    totalCapacity: 5,
    maxSpeed: 220,
    kms: 14000
  },
  corvette: {
    model_type: 'Car',
    totalCapacity: 2,
    maxSpeed: 300,
    kms: 50000,
  }
}

function parseInput(input) {
 return Object.entries(input).reduce((acc, [key, value]) => {
    acc[key] = {
      maxSpeed: value.maxSpeed
    };
    return acc;
  }, {})
}

console.log(parseInput(input));

Get object keys using Object.keys(), which will return ['accord', 'ram', corvette']. Then loop using forEach and return a new object with only the maxSpeed attribute.

const obj = { accord: { model_type: 'Car', totalCapacity: 4, maxSpeed:170, kms: 140000 }, ram: { model_type: 'Car', totalCapacity:5, maxSpeed: 220, kms: 14000 }, corvette: { model_type: 'Car', totalCapacity: 2, maxSpeed: 300, kms: 50000 } };
  
let result = {};

Object.keys(obj).forEach(model => {
  result[model] = {};
  result[model] = { maxSpeed: obj[model].maxSpeed};
});

  
console.log(result);

Related