How to convert array data to new data [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have an initalArray that looks like this:
initailArray: ["a", "b", "c", "d"]
Say I want to convert a -> apple, b -> berry c -> cat and d -> dog
so my newArray should look like this:
newArray: ["apple", "berry", "cat", "dog"]
Is there a way I could do this? I think I need to use if or switch. But
don't know exactly how.

you could use map to go over the initialArray, like this:
let initialArray = ["a", "b", "c", "d"];
let newArray = initialArray.map(item => {
switch(item) {
case 'a':
return 'apple';
break;
case 'b':
return 'berry'
break;
case 'c':
return 'cat';
break;
case 'd':
return 'dog';
break;
}
});
Hope this helps.

You can use Array.map to convert each item to the item from a given mapping.
const mapping = {
a: 'apple',
b: 'berry',
c: 'cat',
d: 'dog'
};
const initialArray = ["a", "b", "c", "d"];
const result = initialArray.map(entry => mapping[entry]);
console.log(result);

First of all you need to have a "map" from codes ("a", "b" ecc...) and your values ("apple", "berry" ...):
let myMap = { a: "apple", b: "berry", c: "cat", d:"dog" };
Then you can use the "map" method of Array:
let newArray = initialArray.map((key) => myMap[key]);
let initialArray = ["a","b","c","d"];
let myMap = { a: "apple", b: "berry", c: "cat", d:"dog" };
let newArray = initialArray.map((key) => myMap[key]);
document.write(JSON.stringify(newArray));

You could just use an object as a map that contains each letter and their corresponding values.
Then you could use Array#map and for each letter return the respective value from the map.
/* #type {{[letter: string]: string}} Map of letters and corresponding values */
var alphabetMap = {
"a": "apple",
"b": "berry",
"c": "cat",
"d": "dog",
};
var initialArray = ["a", "b", "c", "d"];
// transform array of letters to array of respective values
console.log(initialArray.map(function(letter) {
// for each letter return value of `alphabetMap` for that letter
return alphabetMap[letter] || "Invalid Letter"; // Nice to have a default value
}));

const mapping = {
a: 'apple',
b: 'berry',
c: 'cat',
d: 'dog'
};
const result = ["a", "b", "c", "d"].map(ele=>mapping[ele]);
console.log(result);

You can try this
for (i = 0; i < initailArray.length; i++){
if(initailArray[i] == "a"){
initailArray[i] == "apple"
}
else if (initailArray[i] == "b"){
initailArray[i] == "berry"
}
else if (initailArray[i] == "c"){
initailArray[i] == "cat"
}
else if (initailArray[i] == "d"){
initailArray[i] == "dog"
}
}

Related

How to transform Javascript object to an object with key and value array? [duplicate]

This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Closed 2 years ago.
I have data that looks like
[{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}]
and I want to transform it into
{"a":["apple", "alpha"], "b": ["ball"], "c":["cat"]}
you can use a reduce function for this
const arr = [{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}]
const transformed = arr.reduce((acc, cur) => {
acc[cur.letter] = acc[cur.letter] || [];
acc[cur.letter].push(cur.word);
return acc;
}, {});
console.log(transformed);
Something like this you mean?
const arr = [{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}];
let result = {};
for (let e of arr)
(result[e.letter] = result[e.letter] || []).push(e.word);
console.log(result)
Based on the updated outcome. Below is my approach
var elements = [{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}]
output = {}
elements.forEach(function(e){
key = e['letter']
value = e['word']
if(output[key]){
output[key] = [...output[key], value];
}else{
output[key] = [value];
}
})
console.log(output);

How to get the key for a array with his value is equal True

I'm trying to get all the keys on my Object with a value "T" (true) and show them in the component but i'm getting troubles.
I tried with for and forEach but I can't get the keys.
This is my render method and this is the object
render(){
const races = this.state.data.racesTrack.Races;
const racesList = [];
}
I need to see it like this
exacta
hq
place:
quinella
show:
spr:
trifecta:
wps:
Image with the object: https://i.stack.imgur.com/en09V.png
You can do something like this in ES6:
var raceList = []
var races = {
a: "T",
b: "F",
c: "T",
d: "F"
}
for (key in races) {
if (races[key] == "T") {
raceList.push(key)
}
}
console.log(raceList)
The problem you are asking is not related to Reactjs and is about the basics of javascript.
A simpler solution can be using Object Keys Array and the Filter method:
var race = {
a: "T",
b: "F",
c: "T",
d: "F"
}
var sort = Object.keys(race).filter((key)=>{
return race[key]==='T'
})
console.log(sort)

Destructuring for get the first property of an object?

For arrays, we can define the properties depending on it's indexes like:
const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third)
I'm just wondering if there's a possible solution to do it's reverse with objects like:
const obj = {first: "a", second: "b", third: "c"}
const {0, 1, 2} = obj;
//expected: "a", "b", "c"
You do it like this for objects:
const obj = {foo: 123, bar: 'str'}
const {foo, bar} = obj
It isn't.
Objects are not designed to be ordered, so there isn't a first property per se.
You could convert an object into an array of its values first …
const obj = {
first: "a",
second: "b",
third: "c"
}
const array = Object.values(obj);
const [foo, bar, baz] = array;
console.log({
foo,
bar,
baz
});
… but it is unlikely to be useful and it certainly wouldn't be intuitive code that is easy to maintain.
Try this:
const obj = {first: "a", second: "b", third: "c"}
const indexes = [0, 1, 2]
indexes.map( (val) => { return Object.values(obj)[val] } ) //["a", "b", "c"]
You could take the values and assign this to an array for destructuring.
The order is actually dtermined by the insertation order or if a key is like a valid index of an array, it is sorted numerically to top.
const
object = { first: "a", second: "b", third: "c" },
[first, second, third] = Object.values(object);
console.log(first, second, third);
For extracting a an arbitrary position, you vould take an object with an index an object property assignment pattern [YDKJS: ES6 & Beyond] for a new valid variable.
const
object = { first: "a", second: "b", third: "c" },
{ 2: foo } = Object.values(object);
console.log(foo);

Convert an array to nested object using Lodash, is it possible?

I have an array:
["a", "b", "c", "d"]
I need to convert it to an object, but in this format:
a: {
b: {
c: {
d: 'some value'
}
}
}
if var common = ["a", "b", "c", "d"], I tried:
var objTest = _.indexBy(common, function(key) {
return key;
}
);
But this just results in:
[object Object] {
a: "a",
b: "b",
c: "c",
d: "d"
}
Since you're looking for a single object out of an array, using _.reduce or _.reduceRight is a good candidate for getting the job done. Let's explore that.
In this case, it's going to be hard to work from left to right, because it will require doing recursion to get to the innermost object and then working outward again. So let's try _.reduceRight:
var common = ["a", "b", "c", "d"];
var innerValue = "some value";
_.reduceRight(common, function (memo, arrayValue) {
// Construct the object to be returned.
var obj = {};
// Set the new key (arrayValue being the key name) and value (the object so far, memo):
obj[arrayValue] = memo;
// Return the newly-built object.
return obj;
}, innerValue);
Here's a JSFiddle proving that this works.

Sorting an Array of JavaScript Objects a Specific Order (using existing function)

Given an array of objects:
{
key: "a",
value: 42
},
{
key: "d",
value: 28
},
{
key: "c",
value: 92
},
{
key: "b",
value: 87
}
and an array of keys:
["c", "a", "b", "d"]
Is there a ECMAScript function or a 3rd-party JavaScript library that lets you sort - in one line/function call - the first array of objects, to match the order of the keys specified in the second array, such that the result is:
{
key: "c",
value: 92
},
{
key: "a",
value: 42
},
{
key: "b",
value: 87
},
{
key: "d",
value: 28
}
Other questions that provide a function or algorithm:
Javascript - sort array based on another array - Stack Overflow
javascript - How do I sort an array of objects based on the ordering of another array? - Stack Overflow
Similar/related questions:
Sorting an Array of Objects in PHP In a Specific Order
php - Sort array of objects
Just use indexOf to convert the key to the correct order:
var order = ["c", "a", "b", "d"];
_.sortBy(arr, function(obj){
return _.indexOf(order, obj.key);
});
Fiddle
If there are a lot of keys, then it would be advantageous to make a hash-map out of the array, like:
var order = ["c", "a", "b", "d"];
var orderMap = {};
_.each(order, function(i) { orderMap[i] = _.indexOf(order, i); });
This makes the key-sorting lookup constant time rather than O(n). (Fiddle)
Great answers provided so far. Thought that the following may also be an alternative solution in plain JS:
var arr = arr.sort(function(a,b) {
return order.indexOf( a.key ) - order.indexOf( b.key );
//for the sake of recent versions of Google Chrome use:
//return a.key.charCodeAt(0) > b.key.charCodeAt(0); or return a.key.charCodeAt(0) - b.key.charCodeAt(0);
});
var arr = [
{
key: "a",
value: 42
},
{
key: "d",
value: 28
},
{
key: "c",
value: 92
},
{
key: "b",
value: 87
}
];
var order = ["c", "a", "b", "d"];
console.log( 'Original: ', JSON.stringify( arr ) );
var arr = arr.sort(function(a,b) {
return order.indexOf( a.key ) - order.indexOf( b.key );
});
console.log( 'Ordered: ', JSON.stringify( arr ) );
const obj = [
{
key: "a",
value: 42
},
{
key: "d",
value: 28
},
{
key: "c",
value: 92
},
{
key: "b",
value: 87
}
]
const sortList = ["c", "a", "b", "d"];
const sortedObj = obj.sort((a, b) => {
return (
sortList.indexOf(a.key) - sortList.indexOf(b.key)
);
});
console.log(sortedObj );
I can't claim that this is the most efficient way, but you can use the key for each object as a key for properties in another object. Then simply access them by these keys.
for (x = 0; x < objn.length; x++) {
newobj[objn[x].key] = objn[x];
}
objn = [];
for (x = 0; x < keys.length; x++) {
objn.push(newobj[keys[x]]);
}
console.log(objn);
http://jsfiddle.net/WdehF/
// create hash map el.key -> index, to help us with direct access, avoid searching
const hashMap = arr.reduce((acc, el, index) => { acc[el.id] = el; return acc }, {})
// finally, map the ids to the final result
const ids.map(id => hashMap[id])
const data = [{key:"a"},{key:"d"},{key:"c"},{key:"b"}] // <-your data
const order = ["c", "a", "b", "d"] // <-create an array in the order you wish
const orderedArray = order.map(char=>data.find(res=>res.key===char)) // <- what you want
For each char in order: it will map if the char is equal to any key found within your data, and return it, consecutively

Categories

Resources