How can I use an array as map value?
You can't copy arrays by value like that.
Here are several solutions, but I recommend #4 for your needs:
Use an
std::vector
instead of an array.Use a map of pointers to arrays of 3 elements:
int red[3] = {1,0,0};
int green[3] = {0,1,0};
int blue[3] = {0,0,1};
std::map<int,int(*)[3]> colours;
colours.insert(std::pair<int,int(*)[3]>(GLUT_LEFT_BUTTON,&red));
colours.insert(std::pair<int,int(*)[3]>(GLUT_MIDDLE_BUTTON,&blue));
colours.insert(std::pair<int,int(*)[3]>(GLUT_RIGHT_BUTTON,&green));
// Watch out for scope here, you may need to create the arrays on the heap.Use boost tuples instead of arrays of 3 elements.
Instead of using an array make a new struct that takes 3 elements. Make the
map<int, newstructtype>
. Or wrap your array in a struct as follows:struct Triple
{
int color[3];
};
// Later in code
Triple red = {1, 0, 0}, green = {0, 1, 0}, blue = {0, 0, 1};
std::map<int,Triple> colours;
colours.insert(std::pair<int,Triple>(GLUT_LEFT_BUTTON,red));
colours.insert(std::pair<int,Triple>(GLUT_MIDDLE_BUTTON,blue));
colours.insert(std::pair<int,Triple>(GLUT_RIGHT_BUTTON,green));
Mapping values of an array
You don't need lodash or underscore, Array.prototype.map
can help:
let originArray = [ {Priority: 'low', Status: 'received', Creator: 'ABC' }, {Priority: 'high', Status: 'received', Creator: 'DEF' }];
function getColorFromPriority(priority) { if (priority === 'low') { return 'yellow'; } else if (priority === 'high') { return 'red'; }}
let resultArray = originArray.map(function(o) { o.color = getColorFromPriority(o.Priority); delete o.Priority; return o;});
console.log(resultArray);
Set array as Map values in Javascript
Just a simple object:
var param1 = "myParam1", param2 = "myParam2", value1 = [1, 2, 3], value2 = [3, 4, 5];
var myObject = { [param1]: value1, [param2]: value2};
console.log(myObject);
Using an array as the 'value' part of a Map
Array[Int]
is a type. new Array[Int](3)
is a value. When declaring a Map
you need types, not values: Map[String,Array[Int]]
(1,2,3)
is a tuple (or 3-tuple) but you want an array: Array(1,2,3)
v._1
is the first element of a tuple but you want the first element of an array: v(0)
or v.head
This compiles.
var states = scala.collection.mutable.Map[String,Array[Int]]()
states += ("fname" -> Array(1,2,3))
states += ("lname" -> Array(4,5,6))
for ((k,v) <- states) printf("key: %s, value: %s, 0: %s\n", k, v, v(0))
How do I use map() to assign array elements to different variables?
I think you can use .map()
for this task, but not on the jsonData
array itself, but on the array of the properties of your row object.
// This assumes that:
// 1. The order of values in jsonData matches
// with the order of properties in the row object.
// 2. The row object already has properties (has columns)
// but no values yet (undefined or null)
const assignedRowEntries = Object.entries(rowObject).map(
[key, value], index => [key, jsonData[index]]
);
console.log(assignedRowEntries);
// assignedRowEntries should look like this:
// [
// [parts_id, value1],
// [parts_material, value2],
// ...
// [workorder_total, value3]
// ]
// This is what you needed
const assignedRowObject = Object.fromEntries(assignedRowEntries);
More about Object.fromEntries() and Object.entries().
How to convert an array of map into a map in JAVA8?
It seems like each map in the array represents a key-value pair in the resulting map. Therefore, you can use Collectors.toMap
, which accepts to functions - one that transforms an array element to the KVP's key, and one that that transforms an array element to the KVP's value:
public static Map<String, String> mapArraysToMap(Map<String, String>[] mapArray) {
return Arrays.stream(mapArray).collect(Collectors.toMap(
x -> x.get("k"),
x -> x.get("v")
));
}
This is assuming that all array elements have a k
and v
key. You might want to filter
out those that don't if that is possible in your scenario.
How to map values to arrays according to a pre-defined array which contains the last 12 months
I would approach this differently. I keep my months array as in normal order. (Jan first Dec last). That would make the calculations of the other arrays easier. Get the month from date use it as index to fill the arrays. Then calculate the rotation amount from the current month and then rotate your arrays.
var tradesArr = [{ "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-03-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}, { "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-04-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}, { "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-03-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}, { "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-05-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}, { "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-11-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}, { "trades": { "model": "Trades_Information", "fields": { "orderCloseTime": "2019-12-19T16:20:56", "orderOpenPrice": "1.40000", "orderProfit": "75", "orderLots": "1.4" } }}];
// taken from https://stackoverflow.com/a/1985471/12354911Array.prototype.rotate = (function() { var unshift = Array.prototype.unshift, splice = Array.prototype.splice;
return function(count) { var len = this.length >>> 0, count = count >> 0;
unshift.apply(this, splice.call(this, count % len, len)); return this; };})();
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; // use this and keep ordering to later.// since the size is clear create array with size // fill them with 0 to claculate easierNetPipsMonth = Array(12).fill(0);NetProfitMonth = Array(12).fill(0);TotalVolumeMonth = Array(12).fill(0);TotalTradesMonth = Array(12).fill(0);
tradesArr.forEach(t => { const fields = t.trades.fields;
const ix = new Date(fields.orderCloseTime).getMonth(); // zero indexed month NetProfitMonth[ix] += +fields.orderProfit; TotalVolumeMonth[ix] += +fields.orderLots; // continute});
// assume we are on may and we want may to be last item in the array// Date().getMonth() gave us 4 (zero indexed) const rotation = -((12 - 4)-1); // we need to rotate 7 to the right months.rotate(rotation);NetProfitMonth.rotate(rotation);TotalVolumeMonth.rotate(rotation);console.log(months, NetProfitMonth, TotalVolumeMonth);
Map an arbitrary value from an array to a color that exists in another array
Here I build an array of small objects to contain the value and the original index in the values array. Then I sort the new array by value, assign a color to each element by its new index, and re-sort it by original index.
const colors = ["#fb5050", "#f74f4f", "#f34e4e", "#ef4d4d", "#ec4b4b", "#e84a4a", "#e44949", "#e04848", "#dc4646", "#d94545", "#d54444", "#d14343", "#cd4141", "#c94040", "#c63f3f", "#c23e3e", "#be3c3c", "#ba3b3b", "#b63a3a", "#b23838"];
const values = [0.020565500406834823, 0.0006918573709419904, 0.03614457831325302, 0.014884840151254727, 0.9638554216867471, 0.005208333333333333, 0.0006248326341158618, 0.14285714285714285, 0.004872900466547537, 0.8571428571428577, 0, 0.2142857142857144, 0, 0.2499999999999991, 0.5000000004656613, 0.45534591194968543, 0.6349489795918367, 0.25, 0.15218156916454706, 0];
// keep track of the old index and sort by value
const valueMap = values.map((val, i) => ({oldIndex: i, value: val})).sort((a,b) => a.value - b.value);
// add the appropriate color to each element of the sorted array
valueMap.forEach((val, i) => val.color = colors[i]);
//check this console.log to verify it sorted/assigned properly
//console.log(JSON.stringify(valueMap));
// resort it by oldIndex
valueMap.sort((a,b) => a.oldIndex - b.oldIndex);
// get just the array of colors
const newColors = valueMap.map(val => val.color);
console.log(newColors);
Related Topics
How to Use Windbg to Analyze the Crash Dump for Vc++ Application
How to Set the Padding of Qtableview Cells Through CSS
Advantages of Std::For_Each Over for Loop
C++ Equivalent of Stringbuffer/Stringbuilder
C++ - Decimal to Binary Converting
Vector::At VS. Vector::Operator[]
Defining Global Constant in C++
Polymorphic_Allocator: When and Why Should I Use It
Lambda Capture as Const Reference
Osx - Replace Gcc Version 4.2.1 with 4.9 Installed via Homebrew
How to Solve the Error Lnk2019: Unresolved External Symbol - Function