Skip to content

Instantly share code, notes, and snippets.

@victorono
Created October 20, 2016 20:04
Show Gist options
  • Save victorono/34fa63ae0a98dbe1bbcd869a2b6eef6a to your computer and use it in GitHub Desktop.
Save victorono/34fa63ae0a98dbe1bbcd869a2b6eef6a to your computer and use it in GitHub Desktop.
Some minor explanation: - We use Array.prototype.filter() function to filter out the keys that start with `imageIds2 - We use Array.prototype.reduce() to convert an array of filtered keys into an object of key-value pairs. For that we use the initial value of {} (an empty object), fill it and return from every execution step.
var data = {
title: 'fsdfsd',
titleZh: 'fsdfsd',
body: 'fsdf',
bodyZh: 'sdfsdf',
imageIds: '/uploads/tmp/image-3.png',
imageIdsZh: ''
};
var z = Object.keys(data).filter(function(k) {
return k.indexOf('imageIds') == 0;
}).reduce(function(newData, k) {
newData[k] = data[k];
return newData;
}, {});
console.log(z);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment