2

I have this object:

{
  nameErr: "Name is required"
  numberErr: "Number is required"
  emailErr: "Email is required"
  genderErr: "Gender is required"
  messageErr: "Message is required"
}

And this array:

var errorVars = [errorName, errorNumber, errorEmail, errorGender, errorMessage]

How can I assign the value of the property nameErr to the errorVars variable and do the same to the rest variables in the array, is it possible?

Something like:

for(i=0; i<errorVars.length; i++){
  errorVars[i] = "property value[i]"
}

PS: Using pure JavaScript.

2
  • You are using two objects. errorVars is going to be object after you do what you want to do, which leads to useless copying, why don't you use first object?. Commented Jul 24, 2017 at 23:44
  • @Oen44 I want to print the properties in html tags. If I have this propertie {name:"my name"} is possible to print "my name" in a span tag in html? Commented Jul 24, 2017 at 23:49

3 Answers 3

2

from comment above your problem is how to select object value, you can select it with obj.keyName or if it dynamic keys use Object.keys(obj)

var obj = {
  nameErr: "Name is required",
  numberErr: "Number is required",
  emailErr: "Email is required",
  genderErr: "Gender is required",
  messageErr: "Message is required"
};
var errorVars = []
// select by key
console.log(obj.nameErr + "\n\n")

// for dynamic key
for(i = 0; i < Object.keys(obj).length; i++) {
  objValue = obj[Object.keys(obj)[i]];
  console.log(objValue)
  // convert to array
  errorVars.push(objValue)
}
console.log(errorVars)

Sign up to request clarification or add additional context in comments.

1 Comment

This is exacly what I was looking for, thanks buddy!
1

Here's a way of converting an object into an array using vanilla JS:

var ob = {
    nameErr: "Name is required",
    numberErr: "Number is required",
    emailErr: "Email is required",
    genderErr: "Gender is required",
    messageErr: "Message is required"
 }

var errorVars = [];

for(var i = 0 ; i < Object.keys(ob).length; i++) {
    var x = Object.keys(ob)[i]
    errorVars.push(x + " : " + ob[x]
};

console.log(errorVars);

Comments

0

It is possible. There are 2 options: 1) you can make it in the same order, so

`var obj ={
nameErr: "Name is required"
//...
}
for(i=0; i<errorVars.length; i++){
  errorVars[i] = obj[i];
}`

2) you need to modify little bit your errorVars structure to

`var errorName ={
      name:"nameErr", value:""
};
var errorVars = [errorName];
for(i in errorVars) {
   errorVars[i].value = obj[errorVars[i].name];
}`

1 Comment

Thanks for the answer @YCotov The first option returns undefined when I console.log, the second one require that I modify my object and I preefer to not do it because is an object form Json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.