0

I've got a Javascript array with a set of objects and I'm trying to remove the key for each of the objects within that array (i.e. the 0s and 1s), however I'm struggling to find a Javascript function that does the trick.

[{
    '0': {
      id: '630640',
      stuff: 'name1',
      anotherProp: 'prop1'
    },
    '1': {
      id: '630640',
      stuff: 'name2',
      anotherProp: 'prop2'
    },
    id: '630640'
  },
  {
    '0': {
      id: '694969',
      stuff: 'name3',
      anotherProp: 'prop3'
    },
    id: '694969'
  },
  undefined
]

I've tried the following but it doesn't remove the keys.

var keys = Object.keys(input),
  output = [];
for (var i = 0, length = keys.length; i < length; ++i)
  output.push(input[keys[i]]);
  
console.log(output);

6
  • 1
    What exactly is the desired output? Do you need to mutate, or do you just want a new data structure? Commented Jul 18, 2019 at 7:22
  • 1
    Have you tried using Object.values? Commented Jul 18, 2019 at 7:26
  • Hi, Thank you for the quick response, the desired response is var input = [ { { id: '630640', stuff: 'name1', anotherProp: 'prop1' }, { id: '630640', stuff: 'name2', anotherProp: 'prop2' }, id: '630640' }, { { id: '694969', stuff: 'name3', anotherProp: 'prop3' }, id: '694969' } ]; Commented Jul 18, 2019 at 7:36
  • Sorry about the formatting.... Trying to work out how to make the code look nicer formatted. Commented Jul 18, 2019 at 7:36
  • @MarkGabb your expected output is not a valid JSON, objects are always in key, value pair. Valid JSON Commented Jul 18, 2019 at 7:43

2 Answers 2

1

You can first filter out all non-null values from arrays then use Object.values to get the values ob each object

let input = [{
	'0': {
		id: '630640',
		stuff: 'name1',
		anotherProp: 'prop1'
	},
	'1': {
		id: '630640',
		stuff: 'name2',
		anotherProp: 'prop2'
	},
	id: '630640'
},
{
	'0': {
		id: '694969',
		stuff: 'name3',
		anotherProp: 'prop3'
	},
	id: '694969'
},
undefined
];

let output = input.filter(nonNull => nonNull).map(obj => Object.values(obj));
console.log(output)

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

Comments

0

You could use Object.values to just get all the values of an object. If you also want to support older browsers you could instead use Object.keys and map the values yourself.

var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);

console.log(input.filter(Boolean).map(function(item) {
  return Object.values(item);
}));

You could also try this format: {630640: [{},{}], 634969: [{}]}

var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);

console.log(input.filter(Boolean).reduce(function(result, item) {
  result[item.id] = Object.values(item).filter(function(property) {
    return typeof property === "object" && property;
  });
  return result;
}, {}));

Or this format: [{id:630640, list: [{},{}]},{id:634969, list: [{}]}]

var input = JSON.parse(`[{"0":{"id":"630640","stuff":"name1","anotherProp":"prop1"},"1":{"id":"630640","stuff":"name2","anotherProp":"prop2"},"id":"630640"},{"0":{"id":"694969","stuff":"name3","anotherProp":"prop3"},"id":"694969"},null]`);

console.log(input.filter(Boolean).map(function(item) {
  return {
    id: item.id,
    list: Object.values(item).filter(function(property) {
      return typeof property === "object" && property;
    })
  };
}));

If you want to support older browser support you can polyfill Object.values like this:

Object.defineProperty(Object, "values", {
  configurable: false,
  writable: false,
  value: function(obj) {
    return Object.keys(obj).map(function(key) {
      return obj[key];
    });
  }
});

4 Comments

Thank you for your prompt response Nick. I've tried your solution but it put the results for 63040 into seperate objects rather than being kept in an overall Object. It's probably my fault for not being clear in my original post about what format I wanted. This is my desired output var input = [ { { id: '630640', stuff: 'name1', anotherProp: 'prop1' }, { id: '630640', stuff: 'name2', anotherProp: 'prop2' }, id: '630640' }, { { id: '694969', stuff: 'name3', anotherProp: 'prop3' }, id: '694969' } ];
@MarkGabb What you wrote isn't valid JSON format. You can't remove the 0 and 1 and keep the id. If you want Object format you will need to have 0 and 1. If you want Array format you will have to remove the id. You can either do [{...},{...},"63040"] or {0:{},1:{},id:"63040"}.
@MarkGabb If you are willing to change the format you could do: {630640:[{...},{...}],694969:[{...}]}
@MarkGabb Please check the updated answer, to see if any of the additional formats work for you.

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.