testDetails.map(function(data){
let JsonData={
title: data["title"],
url: data["url"],
firstName: data["username"],
work:[{workid:data["workid"].slice(","), workname: "tester"}],
employee: [{employee_id: data["employee_id"].slice(","), employee_desk:"custom"}]
}
})
so my above json output look like this
{
"title":"01"
"url":"employe#url.com",
"firstName":"employee name",
"work":[{"workid":"9371", "workname": "tester"}],
"employee":[{"employee_id":"weh34",employee_desk:"custom"}]
}
and another JSON object where data look like this
let data={
"031w":"ewid3728e",
"9371":"emp_01",
"weh34":"work_place01"
}
The workid and employee id with the JsonData I want to replace with the value which is present inside the data json so my data which is look like this
"work":[{"workid":"9371", "workname": "tester"}],
"employee":[{"employee_id":"weh34",employee_desk:"custom"}]
needed to be look like this
"work":[{"workid":"work_place01", "workname": "tester"}],
"employee":[{"employee_id":"emp_01",employee_desk:"custom"}]
I am trying to replace the code manually like this
work:[{workid:data.weh34, workname: "tester"}],
employee: [{employee_id: data.9371, employee_desk:"custom"}]
but data["workid"].slice(",") and data["employee_id"].slice(",") always get changed so I want to match both the json and fetch the value and place it in my JsonData
Just use the value you got from testDetails as the index in the data object to get the corresponding value.
You need to use a different variable from data as the parameter to the mapping function so you can refer to the external variable data. I changed it to d below.
testDetails.map(d => ({
title: d["title"],
url: d["url"],
firstName: d["username"],
work: [{
workid: data[d["workid"].slice(",")],
workname: "tester"
}],
employee: [{
employee_id: data[d["employee_id"].slice(",")],
employee_desk: "custom"
}]
}));
Related
Hello I would like to know if there would be a method to recover the uuid when I push data in my table I put you in the screenshot below
the code
push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
The push function returns a Reference object, from which you can get the key with something like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
console.log(newRef.key);
If you want to use that key in the write operation, you can also separate the creating of the new ID from the writing of the data like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`));
console.log(newRef.key);
set(newRef, {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
In this snippet, the first line is a pure client-side operation that doesn't actually write anything to the database yet.
i have some array looks like this:
const [listArr, setLA]= useState([
{
id: Math.floor(Math.random * 100000),
data: "This is data 1",
},
{
id: Math.floor(Math.random * 100000),
data: "This is data 2",
},
....(and so on)
]);
and i need to extract those data to become 1 variable and get it using console looks like
console.log(data);
//This is data 1\nThis is data2
I've tried using foreach to put 1 by 1 but still didn't work, can you guys help me?
You may use Array#map function to get an array of the values you want and then join them into a string in order to print to the console:
console.log(listArr.map(({ data }) => data).join('\n'))
I have a read-only object that is returned by GraphQL (vue-apollo) query, the result which is read-only looks something like this:
result: {
id: 'yh383hjjf',
regulations: [{ title: 'Test', approved: false}]
})
I want to bind this to a form and be able to edit/update the values in the regulations array and save it back to the database.
at the moment when I try to edit I get the error below:
Uncaught TypeError: "title" is read-only
I tried cloning the result returned by the database using object.assign
//target template
const regulatoryApprovals = {
id: null,
regulations: [{ title: null, approved: null}]
})
regulatoryApprovals = Object.assign(regulatoryApprovals, result, {
regulations: Object.assign(regulatoryApprovals.regulations, result.regulations)
})
but this didn't work.
Does anyone know how I can properly clone the result?
regulatoryApprovals= Object.assign(regulatoryApprovals, ... indicates the problem because regulatoryApprovals is modified with Object.assign, so it would need no assignment.
Read-only regulatoryApprovals object needs to be cloned. regulations is an array and won't be merged correctly with Object.assign, unless it's known that array elements need to be replaced. It should be:
regulatoryApprovals = {
...regulatoryApprovals,
...result,
regulations: [...regulatoryApprovals.regulations, result.regulations]
}
Where { ...regulatoryApprovals, ... } is a shortcut for Object.assign({}, regulatoryApprovals, ...).
Im trying to separate out the functionality of my model and the data so ive created a separate json file with a basic table
when my model builds it creates an object and i need it to create a value in it based on a value coming in:
{
"1":"apple",
"2":"banana",
"3":"orange",
"4":"grape"
}
async save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: (This set by referencing the json, based on the Item code coming in above)enter code here
}
You can import that json object in file where you're having your model, than based on input to function you can get value out of object.
let obj = {"1":"apple","2":"banana","3":"orange","4":"grape"}
function save (xmlOrder) {
let customerOrder = {
ID: xmlOrder.ID,
Name: xmlOrder.Name ,
ItemCode: xmlOrder.ItemCode ,
Fruit: obj[xmlOrder.ItemCode] || 'Not in list',
}
return customerOrder
}
console.log(save({ID:33,Name:'Name',ItemCode:'2'}))
console.log(save({ID:303,Name:'Name1',ItemCode:'21'}))
I have a JSON object that looks like this:
var obj = {
"selection":[
{
"author":"John Doe",
"articles":[
"Article One",
"Article Two"
]
}
]
}
I want to pass this object to Django to render a view that displays 'Article One' and 'Article Two' upon render. I first serialize the JSON object so that it can be appended to a URL; I use $.param(obj) for serialization. Now the JSON object looks something like this:
"selection%5B0%5D%5Bauthor%5D=John+Doe&selection%5B0%5D%5Barticles%5D%5B%5D=Article+One&selection%5B0%5D%5Barticles%5D%5B%5D=Article+Two"
Now I can append this to a path and use window.open(url) the view will handle everything else. On the Django end, I was surprised to see that the structure of the JSON object has changed to this:
"selection[0][author]=John+Doe&selection[0][articles][]=Article+One&selection[0][articles][]=Article+Two"
I want to be able to use the JSON object as a dict e.g.:
obj = request.GET.get('selection')
obj = json.loads(obj)
print(obj[0].author)
...
How should I handle this JSON structure on the Django side of things?
You are not properly serializing the object to JSON, even if you say you do. The correct way would be to use JSON.stringify(), as #dunder states.
Than you parse it back to an object with JSON.parse(strignifiedJson).
var obj = {
"selection":[
{
"author":"John Doe",
"articles":[
"Article One",
"Article Two"
]
}
]
}
// Stringify and encode
var objAsParam = encodeURIComponent(JSON.stringify(obj));
// Send as a param, for example like http://example.com?obj=YourStringifiedObject...
// Parse it back:
var parsedObj = JSON.parse(decodeURIComponent(objAsParam));