Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mutate Key Value Pairs

Input

var obj = {
	app: "one",
	ban: "house"
}

or

var arr = [
  { name: "app", value: "one" },
  { name: "ban", value: "house" }
]

Output

var obj2 = {
	apple: "ONE",
	banana: "home"
}

or

var arr2 = [
  { name: "apple", value: "ONE" },
  { name: "banana", value: "home" }
]

Rules

  • Names are a simple string mapping
  • Values are function

Options

var mappings = {
  "app": { name: "apple", value: (input) => input.toUpperCase() },
  "ban": { name: "banana", value: (input) => ({"home": "house"})[input] }
}

arr.map(old => {
	var map = mappings2[old.name]
	return { name: map.name, value: map.value(old.value) }
})

var mappings = [
  { oldName: "app", newName: "apple", update: (input) => input.toUpperCase() },
  { oldName: "ban", newName: "banana", update: (input) => ({"house": "home"})[input] }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.