2011-02-19 1 views
-1

는 함수의 매개 변수로 데이터 구조를 전달할 수 있습니다 자바 스크립트에서자바 스크립트에서 매개 변수로 전달되는 문장 및 단어의 배열을 얻는 방법은 무엇입니까? 나는 기능이

var init = function(data){ 
    all sentences: " I have to get all the sentences and return an array containing all sentences" 
    all words : "this method should return an array of words in ‘Data’ when there is no parameter passed in. Optionally, when there is a parameter passed in that is a number, return the words in the sentence indicated by the input parameter" 

    all reverse sentences: "this method is the same as all Sentences, except it should return the sentences in reverse order" 

    reverse words :" same as all words but in reverse order" 
    countWordsBeginningWith:" this method should return the amount of words that begin with an inputted string. The optional second parameter should determine what sentence the words come from when present." 

감사

+1

이 숙제입니까? – drudge

+0

여기서 정확히 무엇을하길 원하십니까? –

+0

나는 복잡한 객체를 함수에 전달해야하는 비슷한 종류의 상황이있다. 나는 나와 함께 작동하는 코드를 가지고있다 ... 나는 그것을 최적화 할 수 있는지보고 싶어한다. @ jnpcl this 숙제가 아니다. (아무런 조언이 없다면 말하지 말아라.) ... 내 상황에서 JSON 객체를 서버에서 가져와야하고 특정 키에 접근해야하고 조작해야한다. .. JSON 객체에 대해 걱정하지 않지만 효율적으로 조작하는 것은 문제입니다. – paul

답변

1

argument (문자열이라고 가정)을 array으로 분해해야한다고 생각합니다. 예 :

function getAllWords(sentences) { 
    var result = sentences.split(' '); 
    return result; 
} 

var init = function(data){ 
    var result = []; 
    result['words'] = getAllWords(data.text); 
    // result['sentences'] = getAllSentences(data.text); 
    // result['sentencesreversed'] = getReverseSentences(data.text); 
    // result['sentencewords'] = getReverseWords(data.text); 
    // result['beginswith'] = getWordsBeginningWith(data.text, data.beginswith); 
    return result; 
} 

var getIt = { 
    'beginswith': 't', 
    'text': 'This is stuff. I am a sentence. Stuff happens now.' 
}; 

console.log(init(getIt)); 

이것은 매우 단순한 대답으로 마침표, 쉼표 및 기타 비트는 고려하지 않았습니다. 그러나 그것은 일반적인 대답입니다. 이 시점 이후에 for loops 및/또는 RegEx가 나타날 수 있습니다. 구매자 조심하십시오.

+0

감사합니다 @ 자레드 ... 기본 아이디어를 얻었다. 그걸 기반으로 내 솔루션을 확장하십시오 .. – paul

+0

@Paul - 여기에서 읽을 수 있습니다 : http://www.quirksmode.org/js/associative.html 객체와 배열은 Javascript에서 동일하지만 일반적이지는 않습니다. 그 (것)들을 참조하기 위하여 사용 된 표기법은 상호 교환 할 수 있고, 그렇지 않으면, 누군가는 저를 정정하기를 바란다. –

+0

링크를 주셔서 감사합니다 ... 정보 객체와 배열이 같지 않습니다 ... JS에서 다른 프리미티브입니다 ... 객체 리터럴과 같은 배열에서도 반복 할 수 있지만 ... – paul

0

을 말한다.

먼저 단어 나 문장을 포함하는 문자열 배열을 만드는 법을 배웁니다. 일단 당신이 이것을 성취하면 그것은 간단 할 것입니다.

관련 문제