0

I have a variable list of elements and need to get their names and values in pairs (associative array or object).

So far I have the following but how do I create the pairs of name and value for each element ?

var names = [];
var values = [];

$('.modalField').each(function() {
    fieldName = $(this).attr('name');
    fieldVal = $(this).val();
    
    names.push(fieldName);
    values.push(fieldVal);
});

Thanks for any help,
Tom

3

4 Answers 4

1

Use bracket notation.

var assoc = {};
$('.modalField').each(function() {
    let fieldName = $(this).attr('name');
    let fieldVal = $(this).val();
    assoc[fieldName] = fieldVal;
});

(Also, you should initialize your variables with let/var/const inside the function so they don't leak into the global scope.)

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

3 Comments

...Certainly this subject has come up before?
@HereticMonkey: I would assume so but couldn't it through the search.
This is perfect - thanks a lot ! Regarding the var initialisation, I have done that inside the function, just left it out here.
0

Something like this?

var fields = [];

$('.modalField').each(function() {
    var name = $(this).attr('name');
    var value = $(this).val();
    
    fields.push({ name, value });
});

console.log(fields);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input class="modalField" name="n1" value="v1" />
  <input class="modalField" name="n2" value="v2" />
  <input class="modalField" name="n3" value="v3" />
</div>

Output:

[
  {
    "name": "n1",
    "value": "v1"
  },
  {
    "name": "n2",
    "value": "v2"
  },
  {
    "name": "n3",
    "value": "v3"
  }
]

3 Comments

this isn't an associative array though, and there is a built-in jquery method that does exactly this.
You're right. Didn't notice the associative word. Leaving this answer in in-case someone else misses it as well.
Thanks a lot for this too - it does help too.
0

Instead of having name & values array, I would suggest you to create an object.

var obj ={};
$('.modalField').each(function() {
    obj[$(this).attr('name')] = $(this).val();
   
});

So this way you will have your object like this (names used as example only): {"name1":"val1","name2":"val2","name3":"val3"}

2 Comments

This is very helpful too - I like the approach. Thanks a lot.
NOTE: that is identical to stackoverflow.com/a/63413402/295783 and first example of stackoverflow.com/a/63413444/295783
0

You say name-value but you seem to want an object

You can do EITHER

// Object
const obj = {};
$('.modalField').each(function() {
  const fieldName = $(this).attr('name');
  const fieldVal = $(this).val();
  obj[fieldName] = fieldVal
});

console.log(obj)

// OR Name-Value pair


const arr = $('.modalField').map(function() {
  return {
    [$(this).attr('name')]: $(this).val()
  }
}).get()


console.log(arr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input name="a" value="1" class="modalField" />
<input name="b" value="2" class="modalField" />
<input name="c" value="3" class="modalField" />
<input name="d" value="4" class="modalField" />

Comments

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.