I want to find differences between States in React. But also movements inside the state should be detected. For example, if I deleted the first item:
{"state":[
{"x": 1, "y": 3},
{"x": 4, "y": 5},
{"x": 2, "y": 6},
{"x": 0, "y": 9}
]
}
{"state":[
{"x": 4, "y": 5},
{"x": 2, "y": 6},
{"x": 0, "y": 9}
]
}
I use the jsondiffpatch package, but it works only when the last Item is deleted. but for diff(state, state1) I get this:
{
"state": {
"0": {
"x": [
1,
4
],
"y": [
3,
5
]
},
"1": {
"x": [
4,
2
],
"y": [
5,
6
]
},
"2": {
"x": [
2,
0
],
"y": [
6,
9
]
},
"_t": "a",
"_3": [
{
"x": 0,
"y": 9
},
0,
0
]
}
}
But I want to check if an item was just moved also. So the Difference should be:
{
"state": {
"0": [
{
"x": 1,
"y": 3
},
0,
0
]
}
}
I use the diff and patch from jsondiffpatch for an undo function and the undo state.
Is this possible?
Thank you!
You basically want to get the difference or we can say the intersection of two arrays.
You can use Lodash or Ramda to get your desired output.
Both of them has a variety of util functions available.
Related
I am trying to create a multidimensional array by mapping an array of strings to a 2D array.
var dataFieldArray = ["LegPenetration", "Temperature"];
var multidimensionalArray = [[{"x": 0, "y": 0}, {"x": 10, "y": 20}, {"x": 20, "y": 30}, {"x": 30, "y": 40}, {"x": 40, "y": 50}],
[{"x": 0, "y": 0}, {"x": 10, "y": 200}, {"x": 20, "y": 250}, {"x": 30, "y": 400}, {"x": 40, "y": 450}]]
The expected output should be as follows:
var data = [[{"field": LegPenetration, "x": 0, "y": 0}, {"field": LegPenetration, "x": 10, "y": 20}, {"field": LegPenetration, "x": 20, "y": 30}, {"field": LegPenetration, "x": 30, "y": 40}, {"field": LegPenetration, "x": 40, "y": 50}],
[{"field": Temperature, "x": 0, "y": 0}, {"field": Temperature, "x": 10, "y": 200}, {"field": Temperature, "x": 20, "y": 250}, {"field": Temperature, "x": 30, "y": 400}, {"field": Temperature, "x": 40, "y": 450}]]
In the code below, I have mapped xValueArray and yValueArray together to get the resulting 2D array as shown above. I have tried mapping the dataField array the same way but to no avail. Any help is greatly appreciated!
const yValueArray = [[0, 20, 30, 40, 50], [0, 200, 250, 400, 450]];
const xValueArray = [0, 10, 20, 30, 40];
const data = yValueArray.map(data =>
data.map((d, i) => ({
x: xValueArray[i],
y: d
}))
);
It sounds like you want to add a field property. Here's a way to do that that doesn't modify the original objects using only ES5-level language features and Object.assign (which is ES2015, but polyfillable):
var result = multidimensionalArray.map(function(array, index) {
var field = dataFieldArray[index];
return array.map(function(object) {
return Object.assign({}, object, {field: field});
});
});
Live Example:
var dataFieldArray = ["LegPenetration", "Temperature"];
var multidimensionalArray = [[{"x": 0, "y": 0}, {"x": 10, "y": 20}, {"x": 20, "y": 30}, {"x": 30, "y": 40}, {"x": 40, "y": 50}],
[{"x": 0, "y": 0}, {"x": 10, "y": 200}, {"x": 20, "y": 250}, {"x": 30, "y": 400}, {"x": 40, "y": 450}]];
var result = multidimensionalArray.map(function(array, index) {
var field = dataFieldArray[index];
return array.map(function(object) {
return Object.assign({}, object, {field: field});
});
});
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
Or with ES2015 and ES2018 features:
const result = multidimensionalArray.map((array, index) =>
array.map(object => ({...object, field: dataFieldArray[index]}))
);
Live Example:
const dataFieldArray = ["LegPenetration", "Temperature"];
const multidimensionalArray = [[{"x": 0, "y": 0}, {"x": 10, "y": 20}, {"x": 20, "y": 30}, {"x": 30, "y": 40}, {"x": 40, "y": 50}],
[{"x": 0, "y": 0}, {"x": 10, "y": 200}, {"x": 20, "y": 250}, {"x": 30, "y": 400}, {"x": 40, "y": 450}]];
const result = multidimensionalArray.map((array, index) =>
array.map(object => ({...object, field: dataFieldArray[index]}))
);
console.log(result);
.as-console-wrapper {
max-height: 100% !important;
}
I need to get data from a nested array of objects, given an ID that I have.
I have been trying to get data from it so that I can pass it in to Angular Gridster 2, but even when using array.filter, I am struggling to get the results I want.
Data I start with:
[
{
"0": {
"cols": 15,
"id": "5-1564645705217",
"rows": 7,
"type": 0,
"x": 0,
"y": 0
},
"1": {
"cols": 15,
"id": "5-1564645705217",
"rows": 7,
"type": 1,
"x": 15,
"y": 0
},
"2": {
"cols": 15,
"id": "5-1564645705217",
"rows": 8,
"type": 2,
"x": 0,
"y": 7
},
"id": "1zk66HvI97C03751LNQm"
},
{
"0": {
"cols": 5,
"id": "5-1564545",
"rows": 7,
"type": 0,
"x": 0,
"y": 0
},
"1": {
"cols": 5,
"id": "5-1564545",
"rows": 7,
"type": 1,
"x": 15,
"y": 0
},
"id": "8gdfg897C03751LNQm"
}
]
I have an id (such as 1zk66HvI97C03751LNQm) and want to be able to fetch the contents so that I end up with:
[
{
"cols": 15,
"id": "5-1564645705217",
"rows": 7,
"type": 0,
"x": 0,
"y": 0
},
{
"cols": 15,
"id": "5-1564645705217",
"rows": 7,
"type": 1,
"x": 15,
"y": 0
},
{
"cols": 15,
"id": "5-1564645705217",
"rows": 8,
"type": 2,
"x": 0,
"y": 7
}
]
Using Array.prototype.find you can easily locate element you want (granted it will only return first found entry, so if your id can be non unique you should use filter instead) after which i remove id from the found object, and turn the rest of the data into desired format.
const data = [{"0": {"cols": 15, "id": "5-1564645705217", "rows": 7, "type": 0, "x": 0, "y": 0}, "1": {"cols": 15, "id": "5-1564645705217", "rows": 7, "type": 1, "x": 15, "y": 0}, "2": {"cols": 15, "id": "5-1564645705217", "rows": 8, "type": 2, "x": 0, "y": 7}, "id": "1zk66HvI97C03751LNQm"}, {"0": {"cols": 5, "id": "5-1564545", "rows": 7, "type": 0, "x": 0, "y": 0}, "1": {"cols": 5, "id": "5-1564545", "rows": 7, "type": 1, "x": 15, "y": 0}, "id": "8gdfg897C03751LNQm"}]
const searchId = "1zk66HvI97C03751LNQm";
const {id, ...elementFound} = data.find(({id})=> id === searchId) || {}; // skip id as unnecessary, return empty object in case of no entries matching search criteria
const elementParsed = Object.values(elementFound); // get values of other fields
console.log(elementParsed);
For the following
array1 = [{
"x": 0,
"y": 1,
"z": 1,
"i": "chart1"
}, {
"x": 0,
"y": 2,
"z": 1,
"i": "chart2"
}, {
"x": 1,
"y": 1,
"z": 1,
"i": "chart3 "
}
]
array2 = [{
"x": 1,
"y": 1,
"z": 1,
"i": "chart1"
}, {
"x": 0,
"y": 1,
"z": 1,
"i": "chart2"
}, {
"x": 0,
"y": 2,
"z": 1,
"i": "chart3"
}
]
compare x and y of array1 and array2 and find the position if it is equal and return the set of array of i value.
i.e in above case it should return:
array3=["chart2","chart3","chart1"]
I have a json as below:
json = [{
"visType:" bar "," visName ":" chart1 "},{" visType: "bar",
"visName": "chart2"
}, {
"visType:" Pie "," visName ":" chart3 "}]
And this need to be sort based on array3 = ["chart2","chart3","chart1"] the output should be as in updated json
updatedjson = [{
"visType:" bar "," visName ":" chart2 "},{" visType: "Pie",
"visName": "chart3"
}, {
"visType:" bar "," visName ":" chart1 "}]
I need a solution using lodash or javascript.
const array3 = [...array1, ...array2].filter(({x,y}) => x===y);
const updatedjson = array3.map(({i}) => json.find(({visName}) => i === visName));
Edit: Fixed the input data which was full of syntax errors:
var array1 = [{ "x": 0, "y": 1, "z": 1, "i": "chart1" }, { "x": 0, "y": 2, "z": 1, "i": "chart2" }, { "x": 1, "y": 1, "z": 1, "i": "chart3" }];
var array2 = [{ "x": 1, "y": 1, "z": 1, "i": "chart1" }, { "x": 0, "y": 1, "z": 1, "i": "chart2" }, { "x": 0, "y": 2, "z": 1, "i": "chart3" }];
var json = [
{ "visType": "bar", "visName": "chart1" },
{ "visType": "bar", "visName": "chart2" },
{ "visType": "Pie", "visName": "chart3" }
];
const array3 = [...array1, ...array2].filter(({ x, y }) => x === y);
const updatedjson = array3.map(({ i }) => json.find(({ visName }) => i === visName));
console.log(array3);
console.log(updatedjson);
I'd like to delete an object element from an object. I have tried the following but it returns the same without deleting an element. jsfiddle. I am trying to delete clock object.
How can I delete it?
var position = '{"weather":{"id":"weather","x":0,"y":0,"width":12,"height":9},"google_calendar":{"id":"google_calendar","x":0,"y":10,"width":12,"height":9},"clock":{"id":"clock","x":0,"y":19,"width":3,"height":3},"todo":{"id":"todo","x":3,"y":19,"width":6,"height":4}}';
var name = "clock";
console.log(position);//before delete
delete position.name;
//delete position.name;
console.log(position);//after delete
I'd like to achieve this.
{"weather":{"id":"weather","x":0,"y":0,"width":12,"height":9},
"google_calendar{"id":"google_calendar","x":0,"y":10,"width":12,"height":9},
"todo":{"id":"todo","x":3,"y":19,"width":6,"height":4}}
First off position is a string, not an object.
Second off, position.name operates on the .name property. If you want to operate on the property whose name is in the name variable, then you use position[name], not position.name.
So, if you remove the quotes from the declaration of position or call JSON.parse() on it to make it into an object so it's this:
var position = {
"weather": {
"id": "weather",
"x": 0,
"y": 0,
"width": 12,
"height": 9
},
"google_calendar": {
"id": "google_calendar",
"x": 0,
"y": 10,
"width": 12,
"height": 9
},
"clock": {
"id": "clock",
"x": 0,
"y": 19,
"width": 3,
"height": 3
},
"todo": {
"id": "todo",
"x": 3,
"y": 19,
"width": 6,
"height": 4
}
};
Then, you can do this:
var name = 'clock';
delete position[name];
console.log(position);
And, that will end up deleting the clock property from your object.
var position = {
"weather": {
"id": "weather",
"x": 0,
"y": 0,
"width": 12,
"height": 9
},
"google_calendar": {
"id": "google_calendar",
"x": 0,
"y": 10,
"width": 12,
"height": 9
},
"clock": {
"id": "clock",
"x": 0,
"y": 19,
"width": 3,
"height": 3
},
"todo": {
"id": "todo",
"x": 3,
"y": 19,
"width": 6,
"height": 4
}
};
Below statement will do your job.
delete position["clock"]
I have json format which I want to create in java code using JSONObject and JSONArray but I did not get output in proper format. JSON format is as below.
var transaction_Data =
[
{
"key": "PASSED",
"values": [
{"x": "20 June", "y": 30},
{"x": "21 June", "y": 50},
{"x": "22 June", "y": 20},
{"x": "23 June", "y": 60},
{"x": "19 June", "y": 20},
{"x": "24 June", "y": 10}
]
},
{
"key": "FAILED",
"values": [
{"x": "19 June", "y": 50},
{"x": "21 June", "y": 30},
{"x": "20 June", "y": 20},
{"x": "23 June", "y": 70},
{"x": "22 June", "y": 45},
{"x": "24 June", "y": 60}
]
}
]
How can I create this json object in java because I want to use this object for creating multibar graph using NVD3. Any help is greatly appreciated!
you can try it out with this POJOs.
class TransactionData {
private String key;
private List<Data> values;
public TransactionData(String key, List<Data> values) {
this.key = key;
this.values = values;
}
}
class Data {
private String x;
private Integer y;
public Data(String x, Integer y) {
this.x = x;
this.y = y;
}
}