Building a VUEJS project - I have computed property being returned from the store. An array of objects.
I want to rename the property name coming from the store (which, behind the scenes is fetching from the API)
For example: What I am getting the response from store/API
{
"f1": "John",
"f2": "Doe",
"f3": "School",
},
{
"f1": "Michael",
"f2": "John",
"f3": "College",
}
]
What I want it to be in the data
[
{
"Nom": "John",
"Prenom": "Doe"
},
{
"Nom": "Michael",
"Prenom": "John"
}
]
I want the computed property to be formatted the way I want it to be.
Also, to remove any unwanted properties coming from the response.
Use Array.map:
computed: {
formatted() {
return json.map(item => ({
Nom: item.f1,
Prenom: item.f2
}));
}
}
Related
I have an array of objects and I need to update a few properties of a specific object inside the array and then run the findAndUpdateById call on it. I am trying to do this but its not updating and gives me an error of undefined name property. I guess I am not following the right procedure to update an update an object of javascript and because of this i need help.
Here is my array
let arr = [
{
"_id": "1234",
"customer": {
"firstName": "John",
"lastName": "Doe",
"email": "johndoe#gmail.com",
"address": "123 Caroline Street"
}
}
]
Now I am recieving parameter like this that i need to update in my object
let ctx = {
"params": {
"changeObject": {
"firstName": "Ali",
"email": "ali#gmail.com"
}
}
}
Given: "I am getting the array of object by making .find call against id and there will be only one customer object in the received array"
// Given
let arr=[{"_id":"1234","customer":{"firstName":"John","lastName":"Doe","email":"johndoe#gmail.com","address":"123 Caroline Street"}}];
let ctx={"params":{"changeObject":{"lastName":"Ali"}}};
arr[0].customer = Object.assign(arr[0].customer, ctx.params.changeObject);
console.log(arr);
I think you were using different properties changePayload and changeObject.
You may want to use the spread operator and map for this as well.
let arr = [
{
"_id": "1234",
"customer": {
"firstName": "John",
"lastName": "Doe",
"email": "johndoe#gmail.com",
"address": "123 Caroline Street"
}
}
];
let ctx = {
"params": {
"changePayload": {
"firstName": "Ali",
"email": "ali#gmail.com"
}
}
}
arr = arr.map( data => ({
...data,
customer: {...data.customer, ...ctx.params.changePayload}
}) );
console.log( arr );
NOTE: This will change all objects in your array, but that is what you were asking for.
I am trying to update through useState a child object with an additional object. I created an example to make this more clear :
https://codesandbox.io/s/affectionate-wescoff-u01x0?file=/src/App.js
The example object looks like :
{
"id": 123,
"books": {
"book": {}
}
}
When I push more data in I want it to look like this :
{
"id": 123,
"books": {
"book": {
"name": "Sirens of Titan",
"author": "Kurt Vonnegut"
},
"book": {
"name": "The Hobbit",
"author": "J.R.R. Tolkein"
}
}
}
At this stage I have it pretty messed up and it looks like :
{
"id":123,
"books":[
{
"0":{
"book":{
},
"sampleData1":{
"book":{
"name":"Sirens of Titan",
"author":"Kurt Vonnegut"
}
}
},
"sampleData2":{
"book":{
"name":"The Hobbit",
"author":"J.R.R. Tolkein"
}
}
}
]
}
This is the way I set that broken object :
const [main, setMain] = useState(library);
function addNestedObj() {
setMain({ ...main, books: [{ ...main.books, sampleData1 }] });
}
Just take the destructuring a stage further:
setMain({...main, kids: [...main.kids, secondObj]})
The books property of your library object was an object, not an array. This might have been necessary but I guessed that it isn't since your book objects already have a name property, so they don't need a separate key.
With that change, you can modify your setMain function to add the book property of the sampleData to the books property of your state:
setMain({ ...main, books: [...main.books, sampleData1.book] });
I've added these changes in a fork of your CodeSandbox: https://codesandbox.io/s/modest-fog-byegh?file=/src/App.js
I am trying to take JSON data information from a separate file and insert that info into a newly created object. I know there are a lot of similar questions, but how do you do this with Node JS? I've tried just about all the options from the following link with no success
Add new element to an existing object
Ex:
"EXTERNAL-DATA" : [
{
"clients" {
{
"firstName" : "Thomas",
"lastName" : "Johnson",
"profession" : "Carpenter",
"age": 27,
"education": {
"Diploma": true
}
},
{
"firstName" : "Jane",
"lastName" : "Simpson",
"profession" : "Teacher",
"age": 32,
"education": {
"Masters Degree": true,
"College Degree": true,
"Diploma": true
}
}
}
}
]
If I only want to capture, say firstName, lastName, and education and place each client in a new, empty object collectedData as their own individual objects, how would I go about doing that?
Assuming the EXTERNAL DATA is held inside a variable called data
I've tried the spread operator collectedData = {... collectedData[i], ...data[i]}, but it doesn't add each as new objects
I've tried using new Object(); each time I loop through, but I'm not sure how the left-hand side is supposed to look when there are thousands of objects from the external JSON file.
I've tried Object.assign(), but with no luck
I know I'm overthinking it, but does anybody have an idea even for what direction to go in? I feel burned out.
Would eventually look like this:
collectedData = {
{
"firstName": "Thomas",
"lastName": "Johnson",
"education": {
"Diploma": true
}
},
{
"firstName": "Jane",
"lastName": "Simpson",
"education": {
"Masters Degree": true,
"College Degree": true,
"Diploma": true
}
}
}
You can use Array.map() to go through the clients array (which, looks like a typo in your code... I'm assuming it's an array) and get the object the way you want it.
const collectedData = clients.map((client) => {
return {
firstName: client.firstName,
lastName: client.lastName,
education: client.education
};
});
This basically loops through each client and creates a new object for each one with only the properties you want. Then, a whole new array is returned and assigned to collectedData.
I am playing around with JSON objects in JSON arrays. On click of a button, I push the json objects into a array like below:
jsonArray.push({
columnNameProperty: columnName,
columnValueProerty: columnValue,
id: column.id
});
My resulted array looks like this:
[
0:{
columnNameProperty: "Name",
columnValueProperty: "Nancy",
id: "123"
},
1:{
columnNameProperty: "Name",
columnValueProperty: "Jene",
id: "124"
},
2:{
columnNameProperty: "Amount",
columnValueProperty: "1000",
id: "123"
},
3:{
columnNameProperty: "State",
columnValueProperty: "WA",
id: "123"
}
]
How do I modify this as I want to push items based on the id.
[
"123" : {
"Name" : "Nancy",
"Amount" : "1000",
"State" : "WA"
},
"124" : {
"Name" : "Jene"
}
]
Anyone could suggest me how to structure it in this format.
#Amy is correct, that is not in fact valid javascript. Arrays do not have keys. So your example
[
0:{
columnNameProperty: "Name",
columnValueProperty: "Nancy",
id: "123"
},
1:{
columnNameProperty: "Name",
columnValueProperty: "Jene",
id: "124"
}
]
really looks like this
[
{
columnNameProperty: "Name",
columnValueProperty: "Nancy",
id: "123"
},
{
columnNameProperty: "Name",
columnValueProperty: "Jene",
id: "124"
}
]
If your goal is to retrieve an element by id you could make a function that loops through the array, finds and returns the object with the given id.
Alternatively, you could create a hash map and access each values by its key. So for instance, given this object:
let map = {
"123" : {
"Name" : "Nancy",
"Amount" : "1000",
"State" : "WA"
},
"124" : {
"Name" : "Jene"
}
}
You could get the value of the key "123" by saying map['123']
Why do you have to use an array? For what you are trying to achieve you can set up a object and then just insert more objects into it.
var exampleObject={};
function onClick(){
exampleObject["123"]={"Name":"steve"}
}
I assume you are trying to use that approach to later find the right object in the array?
You can simply loop over the object and find it in there:
for (var obj in exampleObject){
if(obj==="123"){
//do something
}
}
Was able to achieve the required format by creating HashMap/Object:
var id = column.id;
var mapObject = {}, editMap = {};
if(editMap.hasOwnProperty(id)){
mapObject = editMap[id];
mapObject[columnName] = grid[columnName];
editMap[id] = mapObject;
}
else{
mapObject[columnName] = [columnName];
editMap[id] = mapObject;
}
I'm using the mongodb driver for node.js to query my MongoDB document store.
I have the following data in a document store named companies:
{
companyName: "My Company",
users: [
{
first: "Nick",
last: "Kewney",
email: test#user.com,
username: "this.username",
company: "Test Company",
}
],
_id: ObjectId("54a0831fcad79dbf082d65e0")
}
I want to query the store and find any users with the username 'this.username' and return the first.
My attempt below returns the single result but as an array.
db.companies.findOne({ "users.username": "this.username" }, {_id: 0, 'users.$': 1}, next);
Returns...
{
"users": [
{
"first": "Nick",
"last": "Kewney",
"email": "test#test.com",
"username": "this.username",
"company": "Test Company"
}
]
}
My desired result is only the first item, e.g.
{
"first": "Nick",
"last": "Kewney",
"email": "test#test.com",
"username": "this.username",
"company": "Test Company"
}
There are limitations to what can be done with the basic projection operations available to the .find() method of MongoDB. ,findOne() essentially just wraps .find() to return a single document and not a cursor. But these are basically the same.
If you want first manipulation in the server returned result, you need the .aggregate() method. This has a more detailed $project stage than can do further manipulation, along with other stage operators to get the result:
db.companies.aggregate(
[
// Query argument to match document
{ "$match": {
"users.username": "this.username"
}},
// Flatten array out
{ "$unwind": "$users" },
// Filter array
{ "$match": {
"users.username": "this.username"
}},
// Project wanted fields
{ "$project": {
"_id": 0,
"first": "$users.first",
"last": "$users.last",
"username": "$users.username",
"email": "$users.email",
"company": "$users.company"
}}
],
function(err,result) {
}
);
That will give you the result with a restructured document, and also return possible matches for multiple array items as separate results if you needed to.
But for my money, generally just live with what you get from the basic projection. Sure it's nested and also still an array with only 1 item in it, but this is easy to code to manipulate your response. Especially if you truly are using .findOne() in your intended code, as one result is not hard to manipulate there.