Trying to parse mutiple array of strings to numbers - javascript

Is there a way I can iterate through each Oracle endpnt array and parse the strings to numbers and
still keep the Oracle endpnt arrays in order.
Code
var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]
function Oracle(name, endpnt) {
this.name = name;
this.endpnt = endpnt
}
var oracles = []
for(var i=0;i<oracleTitle.length;i++) {
oracles.push(new Oracle(oracleTitle[i],oracleEndpnt[i]))
}
console.log(oracles)
Result
[
Oracle { name: 'oracle1', endpnt: [ '1', '3' ] },
Oracle { name: 'oracle2', endpnt: [ '1', '2' ] },
Oracle { name: 'oracle3', endpnt: [ '1', '3' ] },
Oracle { name: 'oracle4', endpnt: [] }
]

If you dont want to change all the code, you can run a map for the second array
var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]
function Oracle(name, endpnt) {
this.name = name;
this.endpnt = endpnt
}
var oracles = []
for(var i=0;i<oracleTitle.length;i++) {
// Change this part
oracles.push(new Oracle(oracleTitle[i],oracleEndpnt[i].map(num => parseInt(num))));
}
console.log(oracles)

If I understand correctly this should do:
var oracles = [
{ name: 'oracle1', endpnt: [ '1', '3' ] },
{ name: 'oracle2', endpnt: [ '1', '2' ] },
{ name: 'oracle3', endpnt: [ '1', '3' ] },
{ name: 'oracle4', endpnt: [] }
]
var r = oracles.map(x => {
x.endpnt = x.endpnt.map(z => Number(z))
return x
})
console.log(r)

var oracleTitle= ['oracle1','oracle2','oracle3','oracle4']
var oracleEndpnt = [['1','3'],['1','2'],['1','3'],[]]
function Oracle(name, endpnt) { this.name = name; this.endpnt = endpnt }
var oracles = oracleTitle.map((s, i) => new Oracle(s, oracleEndpnt[i].map(Number)))
console.log( JSON.stringify(oracles) )

Related

How to check if a value in an array is present in other object and accordingly return a new object

I have an array
const dataCheck = ["Rohit","Ravi"];
I have another array of object
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
I want to check if any value in dataCheck is present in the userData and then return a new array with the below data
const newData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit", status: "present" },
{ name: "Ravi", status: "present" },
];
I tried to do something using loops but not getting the expected results
const dataCheck = ["Rohit", "Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" }
];
let newDataValue = {};
let newData = [];
userData.forEach((user) => {
const name = user.name;
dataCheck.forEach((userName) => {
if (name === userName) {
newDataValue = {
name: name,
status: "present"
};
} else {
newDataValue = {
name: name
};
}
newData.push(newDataValue);
});
});
console.log(newData);
My trial gives me repeated results multiple results which is just duplicates
You should use map() and a Set.
const dataCheck = ["Rohit","Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
const set = new Set(dataCheck);
const output = userData.map(data => set.has(data.name) ? ({...data, status: "present"}): data)
console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }
A Set allows for lookups in O(1) time and therefore this algorithm works in O(n) time. If you would use the array for lookups (e.g. using indcludes(), find() etc.) the runtime would be O(n²). Although this will certainly not matter at all for such small arrays, it will become more relevant the larger the array gets.
map() is used here because you want a 1:1 mapping of inputs to outputs. The only thing to determine then is, what the output should be. It is either the input, if the value is not in the Set, or it is the input extended by one property status set to "present". You can check for the presence in a Set using the has() method and can use the ternary operator ? to make the decision which case it is.
const dataCheck = ["Rohit", "Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
// map through every object and check if name property
// exists in data check with help of filter.
// if it exists the length of filter should be 1 so
// you should return { name: el.name, status: "present" } else
// return { name: el.name }
let newData = userData.map((el) => {
if (dataCheck.filter((name) => name === el.name).length > 0) {
return { name: el.name, status: "present" };
} else {
return { name: el.name };
}
});
console.log("newdata: ", newData);
A better approach would be to use map over userData array, find for matching element in dataCheck, if found return matching element + a status key or just return the found element as it is.
const dataCheck = ["Rohit","Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
const getUpdatedObject = () => {
return userData.map(userData => {
const userDetail = dataCheck.find(data => userData.name === data);
if(userDetail) return {userDetail, status:"present"}
else return {...userData}
});
}
console.log(getUpdatedObject())
Working fiddle
Loop through userData, check if name is includes in dataCheck. If true add status 'present'.
const dataCheck = ["Rohit","Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
for (let user of userData) {
if(dataCheck.includes(user.name)) {
user.status = 'present'
}
}
console.log(userData)
You are seeing repeated results due to the second loop dataCheck.forEach((userName) => { as every loop of dataCheck will fire the if/else statement and add something to the final array. However many values you add to dataCheck will be however many duplicates you get.
Only need to loop through one array and check if the value is in the other array so no duplicates get added.
const dataCheck = ["Rohit", "Ravi"];
const userData = [{ name: "Sagar" }, { name: "Vishal" }, { name: "Rohit" }, { name: "Ravi" }];
let newDataValue = {};
let newData = [];
// loop thru the users
userData.forEach((user) => {
// set the user
const name = user.name;
// check if in array
if (dataCheck.indexOf(name) >= 0) {
newDataValue = {
name: name,
status: "present",
};
}
// not in array
else {
newDataValue = {
name: name,
};
}
newData.push(newDataValue);
});
console.log(newData);
So you will do like this :
const dataCheck = ["Rohit","Ravi"];
const userData = [
{ name: "Sagar" },
{ name: "Vishal" },
{ name: "Rohit" },
{ name: "Ravi" },
];
const newUserData = userData.map( user => {
dataCheck.forEach( data => {
if( data === user.name )
user.status = "present";
});
return user;
} );
console.log( newUserData );

How to print the values of a array which is in another array insted of "[object]"

So I have an array of "sessions":
var sessions = [];
function Session(title, speaker) {
this.title = title;
this.speaker = speaker;
this.id = nextId;
this.ratings = [];
this.currentRatingId = 0;
this.currentSessionState = SessionState.Created; //SessionState is an enum
}
And the session has an array of ratings:
function Rating(evaluator,ratingValue) {
this.ratingId;
this.ratingValue = ratingValue;
this.evaluator = evaluator;
this.evaluatedSession;
}
If I print my "sessions" array I get this for example:
[Session {
title: 'Javasession',
speaker: 'JavaSpeaker ',
id: 1,
ratings: [[Object], [Object]],
currentRatingId: 2,
currentSessionState: 2
},
Session {
title: 'C#Session',
speaker: 'C#Speaker',
id: 2,
ratings: [[Object]],
currentRatingId: 1,
currentSessionState: 2
}]
As you can see the array which is in the array "session" doesn't print the objects. It prints only "[object]"..
Is it somehow possible to print the values of each array which is in another arrays without using any loops (for,foreach).... ?
You can use JSON#stringify method to print it as a string.
Example:
const arr = [
{
subArr: [
{
subSubArr:[
{
a:1
}
]
}
]
}
];
const res = JSON.stringify(arr);
console.log(res);
You can use console.dir() in browser.
const arr = [
{
subArr: [
{
subSubArr:[
{
a:1
}
]
}
]
}
];
console.dir(arr, { depth: null });
You can use console.dir with { depth: null }
using a simplified array:
let sessions = [ Session: { ratings: [ {Rating: 1}, {Rating: 2} ] } ];
Running console.log(sessions); results in:
[ { Session: { ratings: [Object] } } ]
Running console.dir(sessions, { depth: null}) results in:
[ { Session: { ratings: [ { Rating: 1 }, { Rating: 2 } ] } } ]

Compare 2 arrays in javascript and extract differences

Array 1 is the result of the data from a localstorage
Array 2 is, for the same IDs (329, 307, 355), the result after treatment
So i need to compare both to notify what changed
Array 1 :
[{"329":["45738","45737","45736"]},{"307":["45467","45468"]},{"355":["47921"]}]
Array 2 :
[{"355":["47921","45922"]},{"329":["45738","45737","45736"]},{"307":[]}]
I need to compare Array 2 with Array 1 and extract differences.
In this example i want to have for result
[{"355":["45922"]},{"307":[]}]
I try to adapt this code :
var compareJSON = function(obj1, obj2) {
var ret = {};
for(var i in obj2) {
if(!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
ret[i] = obj2[i];
}
}
return ret;
};
Runnable:
var array1 = [{
"329": ["45738", "45737", "45736"]
}, {
"307": ["45467", "45468"]
}, {
"355": ["47921"]
}],
array2 = [{
"355": ["47921", "45922"]
}, {
"329": ["45738", "45737", "45736"]
}, {
"307": []
}]
var compareJSON = function(obj1, obj2) {
var ret = {};
for (var i in obj2) {
if (!obj1.hasOwnProperty(i) || obj2[i] !== obj1[i]) {
ret[i] = obj2[i];
}
}
return ret;
};
console.log(compareJSON(array1, array2));
But, either I have nothing or I have the whole table
your requirement(result) is not clear, but this will get you started.
var arr1 = [{ "329": ["45738", "45737", "45736"] }, { "307": ["45467", "45468"] }, { "355": ["47921"] }],
arr2 = [{ "355": ["47921", "45922"] }, { "329": ["45738", "45737", "45736"] }, { "307": [] }];
var result = [];
arr2.forEach(obj => {
var key = Object.keys(obj)[0];
var match = arr1.find(o => o.hasOwnProperty(key));
if (match) {
var newObj = {};
newObj[key] = obj[key].filter(s => match[key].indexOf(s) === -1);
if (!obj[key].length || newObj[key].length) result.push(newObj)
} else {
result.push(Object.assign({}, obj));
}
});
console.log(result);
You could use a hash tbale and delete found items. If some items remains, then an empty array is taken to the result object.
var array1 = [{ 329: ["45738", "45737", "45736"] }, { 307: ["45467", "45468"] }, { 355: ["47921"] }],
array2 = [{ 355: ["47921", "45922"] }, { 329: ["45738", "45737", "45736"] }, { 307: [] }],
hash = {},
result = [];
array1.forEach(function (o) {
Object.keys(o).forEach(function (k) {
hash[k] = hash[k] || {};
o[k].forEach(function (a) {
hash[k][a] = true;
});
});
});
array2.forEach(function (o) {
var tempObject = {};
Object.keys(o).forEach(function (k) {
var tempArray = [];
o[k].forEach(function (a) {
if (hash[k][a]) {
delete hash[k][a];
} else {
tempArray.push(a);
}
});
if (tempArray.length || Object.keys(hash[k]).length) {
tempObject[k] = tempArray;
}
});
Object.keys(tempObject).length && result.push(tempObject);
});
console.log(result);
I've used the deep-diff package in npm for this sort of thing before:
It may be more detail than you want though - here's an example from the readme of the output format:
[ { kind: 'E',
path: [ 'name' ],
lhs: 'my object',
rhs: 'updated object' },
{ kind: 'E',
path: [ 'details', 'with', 2 ],
lhs: 'elements',
rhs: 'more' },
{ kind: 'A',
path: [ 'details', 'with' ],
index: 3,
item: { kind: 'N', rhs: 'elements' } },
{ kind: 'A',
path: [ 'details', 'with' ],
index: 4,
item: { kind: 'N', rhs: { than: 'before' } } } ]
Checkout the readme on the github page linked above for details about what it all means, or try it out for yourself online using runkit
But in order for this to work you would have to do some sort of preprocessing:
Sort array based on first key of each element:
a1 = a1.sort((lhs, rhs) => {
return parseInt(Object.keys(lhs)[0]) - parseInt(Object.keys(rhs)[0]);
})
If you sort both of the arrays by the first key of each element and then pass it to the diff tool, you get the following:
[
{"kind":"A","path":[0,"307"],"index":0,"item":{"kind":"D","lhs":"45467"}},
{"kind":"A","path":[0,"307"],"index":1,"item":{"kind":"D","lhs":"45468"}},
{"kind":"A","path":[2,"355"],"index":1,"item":{"kind":"N","rhs":"45922"}}
]
If it were me I would probably merge all the array elements and diff the resulting object so you completely avoid any object order and duplicate key issues.
Alternative: merge array contents into one object
A naive merge might look like this:
a1Object = {}
a1.forEach((element) => {
Object.keys(element).forEach((key) => {
a1Object[key] = element[key];
});
})
Which produces the following diff:
[
{"kind":"A","path":["307"],"index":0,"item":{"kind":"D","lhs":"45467"}},
{"kind":"A","path":["307"],"index":1,"item":{"kind":"D","lhs":"45468"}},
{"kind":"A","path":["355"],"index":1,"item":{"kind":"N","rhs":"45922"}}
]
Interpreting the diff output
there is a change in the Array value of 307 at index 0: 45467 has been Deleted
there is a change in the Array value of 307 at index 1: 45468 has been Deleted
there is a change in the Array value of 355 at index 1: 45467 has been Newly added

convert key value pairs into array javascript

I have an object that I receive from html form, and it has the following syntax:
'ingredients[0][name]': 'eggplant',
'ingredients[0][mass]': '1',
'ingredients[0][units]': 'pc',
'ingredients[1][name]': 'cheese',
'ingredients[1][mass]': '150',
'ingredients[1][units]': 'gr',
'ingredients[2][name]': 'cilantro',
'ingredients[2][mass]': '100',
'ingredients[2][units]': 'tt' ...
All that I need is to convert these key-value pairs into the one big object field. Such as
recipe = {
ingredients: [
{
name: 'epplant',
mass: '1',
units: 'pc'
},
{
name: 'cheese',
mass: '150',
units: 'gr'
}, ...
]
}
How can I do this without JQuery or other JS-framework?
var form = {
'ingredients[0][name]': 'eggplant',
'ingredients[0][mass]': '1',
'ingredients[0][units]': 'pc',
'ingredients[1][name]': 'cheese',
'ingredients[1][mass]': '150',
'ingredients[1][units]': 'gr',
'ingredients[2][name]': 'cilantro',
'ingredients[2][mass]': '100',
'ingredients[2][units]': 'tt'
}
var re = /([a-zA-Z][a-zA-Z_0-9]*)\[(\d+)\]\[([a-zA-Z][a-zA-Z0-9_]*)\]/
var result = {}
for (var key in form) {
var match = key.match(re)
if (!match) continue
var arr = result[match[1]]
if (!arr) arr = result[match[1]] = []
var obj = arr[match[2]]
if (!obj) obj = arr[match[2]] = {}
obj[match[3]] = form[key]
}
console.log(result)
http://jsfiddle.net/r1gkog1b/
UPD: some explanation:
I think, you should iterate throw your input form object keys and parse its with regexp. In case of match you can construct desirable output structure

How can get all objects whose sub object's property matches my string array using linq.js?

I have an array of tag names:
var tags = ['tagOne', 'tagTwo']
Which I want to use, to query the array below and get all items which match a tag.
var items =
[
{
'name': 'itemOne',
'tags': [
{ name: 'tagOne' }
]
},
{
'name': 'itemTwo',
'tags': [
{ name: 'tagTwo' }
]
}
];
How can I do this with linq Js? I.E in this case both items would be returned
Try this; it may not be the most efficient way (I've never used linq.js before) but it will work:
// Enumerate through the items
var matches = Enumerable.From(items)
.Where(function(item) {
// Enumerate through the item's tags
return Enumerable.From(item.tags).Any(function(tag) {
// Find matching tags by name
return Enumerable.From(tags).Contains(tag.name);
})
})
.ToArray();
This should work for you:-
Items
var items =
[
{
'name': 'itemOne',
'tags': [
{ name: 'tagOne' }
]
},
{
'name': 'itemTwo',
'tags': [
{ name: 'tagTwo' }
]
},
{
'name': 'itemThree',
'tags': [
{ name: 'tagThree' }
]
}
];
Tags:-
var tags = ['tagOne', 'tagTwo'];
Search for Tags:-
var fillteredItems = items.filter(function(item){
var tagsInItem = item["tags"];
for (var i = 0; i < tags.length; i++) {
for (var j = 0; j < tagsInItem.length; j++) {
if(tags[i]==tagsInItem[j].name)
return item;
};
};
});
Print Results:-
fillteredItems.forEach(function(item){
console.log("items",item);
})

Categories