-1

I have a function which needs to return either an array or object. I'm not sure which is best and I'm not sure how to construct it. Here's what I have so far. Pay close attention to the comments in ALL CAPS. The idea is to loop through a set of td cells that contain a 'data-field' attribute, and use the name of the attribute as the variable name and the text contained within the td as the value. The number of properties or values of the array/object are unknown. Depending on what was clicked to enter into the function, 2-6 values will be needed.

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

// DEFINE SOME ARRAY OR OBJECT HERE
// put original values in the newEntry Array with a loop, using the 
// data-field attributes as the name of the array or object variables
// and the text of the td as the value
$('#'+rowID+' td[data-field]').each(function() {
    var field = $(this).attr('data-field');
    var value = $(this).text();

    // BUILD OUT OBJECT OR ARRAY
});
// RETURN ARRAY OR OBJECT
}
1
  • Sounds like you need a hashmap/associative array Commented Feb 3, 2011 at 20:52

2 Answers 2

2

I believe something like this would work for your purposes.

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

// DEFINE SOME ARRAY OR OBJECT HERE
var buildup = {};

// put original values in the newEntry Array with a loop, using the 
// data-field attributes as the name of the array or object variables
// and the text of the td as the value
$('#'+rowID+' td[data-field]').each(function() {
    var field = $(this).attr('data-field');
    var value = $(this).text();

    // BUILD OUT OBJECT OR ARRAY
    buildup[field] = value;
});
// RETURN ARRAY OR OBJECT

return buildup;
}
Sign up to request clarification or add additional context in comments.

7 Comments

why is var buildup defined with the curly brackets {} instead of []. I thought {} was for objects and [] for arrays.
Buildup is an object. The notation buildup["foo"] = "Fii" is the same as saying buildup.foo = "Fii".
I didn't know that. I much prefer buildup.foo , but i guess I can't use my field and value variables then... is that right?
Arrays would not be a good container for name-value pairs. Are you wanting to support arrays? How would you expect the name-value pairs to be stored within the array?
I agree that objects are better. I was just trying to do it like this: buildup.field = value which wasn't working
|
1

Try this:

function InlineEditMode(rowID, entryType) {
// store current row data in case of cancel
var original = $('#'+rowID).contents();

var toReturn = {};

// DEFINE SOME ARRAY OR OBJECT HERE
// put original values in the newEntry Array with a loop, using the 
// data-field attributes as the name of the array or object variables
// and the text of the td as the value
$('#'+rowID+' td[data-field]').each(function() {
    var field = $(this).attr('data-field');
    var value = $(this).text();

    toReturn[field] = value;
});
// RETURN ARRAY OR OBJECT
return toReturn;
}

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.